# 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:

```text
Call to a member function update() on null
```

Exception:

```text
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()` and `first()` return null when no row matches, so `Post::find($id)->update([...])` fails for an id that does not exist.
- A guest reached code that needs a user. On a route without the `auth` middleware, `$request->user()` and `auth()->user()` are null for a visitor who is not logged in.
- A `belongsTo` relation has no row. `$post->author->only(['name'])` fails for a post whose `user_id` is 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}` and `Post $post`, Laravel injects an empty `Post`, whose `author` is null. Without a type hint, `$post` is the URL string, and the message ends in `on string`.
- A dependency was never set. An untyped or nullable property such as `private ?Mailer $mailer = null` that nothing assigns gives `Call to a member function send() on null`. A typed property without a default gives a different `Error`: `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
<?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
<?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
<?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
<?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
<?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](/features/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.

Read more: https://www.php.net/manual/en/language.oop5.basic.php

## Related

### Terms

- [Eloquent ORM in Laravel](https://ranetrace.com/glossary/eloquent-orm-in-laravel.md)
- [Route model binding in Laravel](https://ranetrace.com/glossary/route-model-binding-in-laravel.md)
- [Middleware in Laravel](https://ranetrace.com/glossary/middleware-in-laravel.md)

### Other errors

- [Attempt to read property on null](https://ranetrace.com/error-guides/php-attempt-to-read-property-on-null.md)
- [No query results for model](https://ranetrace.com/error-guides/laravel-no-query-results-for-model.md)
- [Call to undefined method](https://ranetrace.com/error-guides/laravel-call-to-undefined-method.md)

Read this guide in a browser: https://ranetrace.com/error-guides/php-call-to-a-member-function-on-null
