# Attempt to read property on null

You read a property of null; its sibling "Call to a member function x() on null" calls a method on null. Decide what null means there, then handle it.

Message:

```text
Attempt to read property "name" on null
```

Exception:

```text
ErrorException
```

## What it means

You wrote `$author->name`, and `$author` was null. Since PHP 8.0 that gives the warning `Attempt to read property "name" on null` and the expression becomes null; PHP 7.2 to 7.4 gave the notice `Trying to get property 'name' of non-object`, and older versions the same notice without the name. Laravel turns every warning into an `ErrorException`, so in a Laravel app the request stops there. Calling a method on null, `$author->getName()`, is stricter: PHP throws an `Error` with the message `Call to a member function getName() on null`, in plain PHP as well, and that wording is the same in PHP 7 and 8.

## Why it happens

- A relation with nothing behind it. `$post->author->name` fails when the post has no `author_id`, because the relation returns null.
- A query that found nothing. `find()` and `first()` return null when no row matches, so `User::where('email', $email)->first()->update([...])` ends in `Call to a member function update() on null`.
- `auth()->user()` for a guest. On a route without the `auth` middleware, a visitor who is not logged in makes `auth()->user()->name` read a property of null.
- A missing value in decoded data. A key that is absent or null in a JSON payload, read with `?? null` and then used as an object.

## How to fix it

Decide first what null means at that spot. If the thing must exist, null is a bug or a bad request, and you should fail early with a clear error. If null is a normal state, handle it where you read it.

When the record must exist, use `findOrFail` or `firstOrFail`. They throw a `ModelNotFoundException`, which Laravel answers with a 404 instead of a 500 a few lines later:

```php
<?php

$post = Post::findOrFail($id);
```

When null is a valid value, the nullsafe operator `?->` (PHP 8.0 and later) returns null and skips the rest of the chain:

```php
<?php

$name = $post->author?->name ?? 'Unknown author';
```

The PHP manual advises keeping `?->` for values that may legitimately be null, and throwing an exception to signal an error. Laravel's `optional()` helper does the same as `?->` and is common in older code:

```php
<?php

$name = optional($post->author)->name;
```

For a `belongsTo`, `hasOne`, `hasOneThrough` or `morphOne` relation, a default model removes the check from every template. `withDefault` returns an empty model, or one with the attributes you give, when the relation is empty:

```php
<?php

class Post extends Model
{
    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class)->withDefault([
            'name' => 'Guest author',
        ]);
    }
}
```

For the logged-in user, put the route behind the `auth` middleware so a guest never reaches the code, or read it as `auth()->user()?->name` where guests are welcome.

For the method call, the same choices apply. Check the result before you call it, or use `?->`, which also works for methods:

```php
<?php

$user = User::where('email', $email)->first();
$user?->update(['last_seen_at' => now()]);
```

## Find it in production

In Laravel with `APP_DEBUG=false`, both messages give the visitor a 500 "Server Error" page. The property version is logged in `storage/logs/laravel.log` as `ErrorException`, the method version as `Error`, both with a stack trace that points at the line. Inside a Blade view the log names `Illuminate\View\ViewException` with the same message, and the `ErrorException` or `Error` as the previous exception. Because Laravel reports both, [Ranetrace error monitoring](/features/error-monitoring) records them with the stack trace and the request they happened on.

Plain PHP behaves differently for the two. The property warning does not stop the script: with `display_errors` off, the page returns 200, the value prints as empty, and the warning only reaches the PHP error log. The method call stops it with `PHP Fatal error: Uncaught Error: Call to a member function getName() on null` and a 500.

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)
- [Middleware in Laravel](https://ranetrace.com/glossary/middleware-in-laravel.md)

### Other errors

- [No query results for model](https://ranetrace.com/error-guides/laravel-no-query-results-for-model.md)
- [Undefined array key](https://ranetrace.com/error-guides/php-undefined-array-key.md)
- [Call to a member function on null](https://ranetrace.com/error-guides/php-call-to-a-member-function-on-null.md)

Read this guide in a browser: https://ranetrace.com/error-guides/php-attempt-to-read-property-on-null
