Call to a member function on null
You called a method on a variable that holds null. Find where the null came from, then fail early with findOrFail or auth, or allow it with ?->.
- Message
- Call to a member function update() on null
- Exception
- Error
What it means
You called a method on a variable that holds null, as in $post->update([...]) when $post is null. PHP throws an Error with the message Call to a member function update() on null, and the script stops there, in plain PHP too. Its sibling Attempt to read property "name" on null is only a warning, which Laravel turns into an exception.
Why it happens
- A query found nothing.
find()andfirst()return null when no row matches, soPost::find($id)->update([...])fails for an id that does not exist. - A guest reached code that needs a user. On a route without the
authmiddleware,$request->user()andauth()->user()are null for a visitor who is not logged in. - A
belongsTorelation has no row.$post->author->only(['name'])fails for a post whoseuser_idis null. - The route parameter and the variable name differ. Implicit binding only resolves a model when the type-hinted variable name matches the route segment. With
/posts/{id}andPost $post, Laravel injects an emptyPost, whoseauthoris null. Without a type hint,$postis the URL string, and the message ends inon string. - A dependency was never set. An untyped or nullable property such as
private ?Mailer $mailer = nullthat nothing assigns givesCall to a member function send() on null. A typed property without a default gives a differentError:Typed property Notifier::$mailer must not be accessed before initialization.
How to fix it
When the record must exist, use findOrFail or firstOrFail. They throw a ModelNotFoundException, which Laravel answers with a 404 instead of a 500:
<?php
$post = Post::findOrFail($id);
$post->update(['title' => $title]);
When the code needs a logged-in user, put the route behind the auth middleware so a guest never reaches it. A guest is redirected to the login route, and a JSON request gets a 401:
<?php
use App\Http\Controllers\SettingsController;
use Illuminate\Support\Facades\Route;
Route::get('/settings', SettingsController::class)->middleware('auth');
When null is a valid state, use the nullsafe operator ?-> (PHP 8.0 and later). It returns null instead of throwing and skips the rest of the chain. The PHP manual advises it for values that may legitimately be null, and an exception for errors:
<?php
$author = $post->author?->only(['name']);
For a belongsTo, hasOne, hasOneThrough or morphOne relation, withDefault() returns an empty model, or one with the attributes you give, instead of null:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Post extends Model
{
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id')->withDefault(['name' => 'Guest author']);
}
}
For route parameters, name the variable after the segment: /posts/{post} with Post $post.
For a dependency, require it in the constructor with a non-nullable type. Laravel's container passes it in, and plain PHP refuses to build the object without it:
<?php
namespace App\Notifications;
use Illuminate\Contracts\Mail\Mailer;
class OrderNotifier
{
public function __construct(private Mailer $mailer) {}
}
Find it in production
In Laravel with APP_DEBUG=false, the visitor gets a 500 "Server Error" page, and a JSON request gets {"message": "Server Error"}. Error is not on the $internalDontReport list in Illuminate\Foundation\Exceptions\Handler, so Laravel reports it: it lands in storage/logs/laravel.log with the file and line, and Ranetrace's error monitoring records it with the stack trace and the request it happened on.
In plain PHP the script stops with PHP Fatal error: Uncaught Error: Call to a member function update() on null, written to the PHP error log. With display_errors off, what the visitor gets depends on output buffering. With output_buffering=4096, PHP's built-in server answered 500. With output_buffering=0, earlier output had already gone out with a 200 status, so the visitor got a 200 page cut off at the failing line.
Reproduced on PHP 8.5, Laravel 13.45, 5 December 2026.
Related
Start monitoring today
Websites go down and code throws errors. Ranetrace tells you when it happens to yours, why it matters and what to do next.