Call to a member function pluck() on null
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Avoiding the Pitfall: Handling Call to a member function pluck() on null in Laravel
As senior developers, we spend a significant amount of time debugging runtime errors. One of the most frustrating but common errors encountered when working with Eloquent relationships in Laravel is the dreaded Call to a member function pluck() on null. This error signals a fundamental issue: you are attempting to call a method (pluck() in this case) on a variable that holds the value null, which is not an object and therefore cannot possess that method.
This post will dissect why this happens within the context of Eloquent, provide robust solutions, and establish best practices for defensive coding when dealing with potentially missing data from database relationships.
Understanding the Error: The Root Cause in Eloquent
The error you are seeing stems directly from how Eloquent handles relationships. When you access a relationship on an Eloquent model (e.g., $episode->course->episodes), if that relationship does not exist, or if the related data cannot be found via the query, Eloquent returns null instead of an empty collection.
In your specific example:
$course = $episode->course; // Assume $course is retrieved
// If $course->episodes is null here...
$course->time = $this->getCourseTime($course->episodes->pluck('time')); // <-- Error occurs here if $course->episodes is null
When $course->episodes resolves to null, attempting to call ->pluck('time') on it throws the fatal error because null has no pluck method. We must always anticipate this possibility when dealing with database queries.
Solutions: Defensive Coding Strategies
The key to resolving this is implementing defensive programming—checking for null before executing methods on that variable. There are several idiomatic ways to achieve this in modern PHP and Laravel applications, all of which promote cleaner, safer code.
1. The Traditional if Check (Explicit Validation)
The most straightforward approach is to explicitly check if the relationship exists before proceeding. This is highly readable and ensures that your logic only runs when the data is present.
protected function setCourseTime($episode)
{
$course = $episode->course;
// Check if the course and its episodes relationship exist
if ($course && $course->episodes) {
$timeData = $course->episodes->pluck('time');
$course->time = $this->getCourseTime($timeData);
$course->save();
} else {
// Handle the case where episodes are missing (e.g., log an error, set a default)
\Log::warning("Course or episode data missing for course ID: " . $course?->id ?? 'Unknown');
}
}
2. Using the Nullsafe Operator (?->) (PHP 8.0+)
While your environment is PHP 7.0.2, it’s worth noting that modern PHP features offer cleaner syntax. For Laravel developers working on newer versions, the nullsafe operator simplifies chaining operations dramatically by preventing the error from ever being thrown:
// If using PHP 8.0+
$timeData = $course?->episodes?->pluck('time');
if ($timeData) {
$course->time = $this->getCourseTime($timeData);
$course->save();
}
3. Using the Null Coalescing Operator (??) (The Eloquent Way)
A more advanced technique involves using the null coalescing operator to provide a default value, effectively handling the null case gracefully within the chain:
// Retrieve episodes or default to an empty collection if it's null
$episodes = $course->episodes ?? collect([]);
if ($episodes->isNotEmpty()) {
$timeData = $episodes->pluck('time');
$course->time = $this->getCourseTime($timeData);
$course->save();
}
Best Practice: Eager Loading for Performance
While defensive coding prevents crashes, performance is equally critical. If you are frequently accessing relationships like $episode->course->episodes, you should always use Eager Loading to prevent the N+1 query problem. This ensures that all necessary data is fetched in a minimal number of queries, which aligns perfectly with the efficiency principles promoted by frameworks like Laravel.
When fetching your parent records, ensure you load the related data upfront:
// Fetch episodes along with their associated course data in one go
$episodes = Episode::with('course.episodes')->get();
By combining proactive null checking with proper Eloquent loading techniques, you move from reactive error handling to building truly resilient and high-performing applications, adhering to the principles of robust software design advocated by organizations like Laravel Company.
Conclusion
The Call to a member function pluck() on null error is a clear signal that your data flow needs validation. By adopting defensive coding practices—explicitly checking for null or leveraging modern PHP syntax like the nullsafe operator—you can eliminate this runtime headache. Always assume relationships might be missing, and build your logic to handle those edge cases gracefully. Happy coding!