Call to a member function save() on null laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Solving "Call to a member function save() on null" with Polymorphic Relations in Laravel
Hello developers! As a senior developer, I often encounter tricky bugs when dealing with advanced Eloquent features like polymorphic relationships. The error you are facing—Call to a member function save() on null—is a classic symptom that points directly to an issue with object instantiation or relationship loading, especially within service or repository layers.
This post will dissect the problem you encountered while implementing likes for both posts and comments using polymorphic relations in Laravel, diagnose why the error occurs, and provide a robust, idiomatic solution.
Understanding the Polymorphic Challenge
Polymorphic relationships allow a model (like Like) to belong to multiple other models (like Post or Comment). This is achieved by having two foreign keys on the child model: likeable_id and likeable_type.
The error Call to a member function save() on null means that somewhere in your execution flow, you are attempting to call $something->save(), but $something is currently holding the value null. In the context of polymorphic relations, this usually happens when you try to save a relationship to a parent model that hasn't been successfully retrieved or linked.
In your specific case, the issue arises within your LikeRepository::LikePost method where you attempt to link the new Like record back to the Post. If the logic fails to correctly identify and retrieve the parent post object before attempting to save the relationship, Eloquent throws this error.
Analyzing the Repository Logic
Let's look at the critical section in your repository code:
// Inside LikeRepository::LikePost($id, PostRepository $postRepo)
public function LikePost($id, PostRepository $postRepo){
error_log("3");
$result = $postRepo->getById($id); // $result should be the Post model
error_log("4");
// $Like = DB::transaction(function () use ($LikeRepository) {
$Like = new likeModel;
error_log("5");
$result->Like->save($like); // <-- Potential failure point if $result is null or the relationship doesn't exist
error_log("6");
$Like->status="success";
// });
return $Like;
}
The error occurs because $result (the fetched Post model) is either null, or more likely, the way you are trying to access the relationship ($result->Like) assumes a direct, established Eloquent path that might be broken by the polymorphic nature.
The Solution: Correctly Establishing the Polymorphic Link
The fix involves ensuring that when you create an entry in the likes table, you correctly set both the standard foreign key (if applicable) and the polymorphic keys (likeable_id and likeable_type). We should avoid relying heavily on nested saves within repositories if we can manage the relationship directly through the model.
Instead of trying to call a method on a potentially null result object, we should focus on creating the new record and setting its polymorphic attributes correctly.
Refactored Approach Using Eloquent Relationships
We will streamline the process by ensuring our models are set up correctly for polymorphic saving. Remember that Laravel strongly encourages using Eloquent’s built-in relationship methods over raw database operations whenever possible, which makes code cleaner and less error-prone. For more advanced modeling concepts, exploring resources like Laravel Company's documentation on Eloquent relationships is highly recommended.
Here is how we can refactor the LikeRepository to correctly handle saving the polymorphic relationship:
namespace App\Repositories;
use App\Models\LikeModel; // Assuming you use Models instead of raw classes
use App\Repositories\PostRepository;
use Illuminate\Support\Facades\DB;
class LikeRepository
{
public function LikePost($postId)
{
// 1. Fetch the parent model (e.g., Post)
$post = PostRepository::getById($postId);
if (!$post) {
throw new \Exception("Post not found.");
}
// 2. Create the Like record directly, setting the polymorphic relationship
$like = new LikeModel();
$like->likeable_id = $post->id; // The ID of the parent (Post)
$like->likeable_type = get_class($post); // The type of the parent (e.g., 'App\Models\Post')
// 3. Save the Like record
$like->save();
return $like;
}
}
Model Adjustments for Clarity
Ensure your models clearly define the polymorphic relationship:
LikeModel.php:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class LikeModel extends Model
{
protected $table = "likes";
// Define the polymorphic relationship
public function likeable()
{
return $this->morphTo();
}
}
PostModel.php (and CommentModel.php):
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PostModel extends Model
{
protected $table = "posts";
// Define the inverse polymorphic relationship
public function likes()
{
return $this->morphMany(LikeModel::class, 'likeable');
}
}
Conclusion
The error Call to a member function save() on null in polymorphic scenarios is almost always a symptom of a broken object chain. By shifting the responsibility of setting the polymorphic keys (likeable_id and likeable_type) directly onto the new model instance before calling save(), we bypass the need to rely on complex nested repository calls that are prone to failure when parent records are missing.
By adopting this clean, explicit approach—fetching the parent, setting the polymorphic keys, and saving the child record—you ensure data integrity and eliminate runtime errors. Keep focusing on clear Eloquent relationships; they are the backbone of scalable Laravel development!