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
- Attempt to read property "name" on null
- Exception
- 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->namefails when the post has noauthor_id, because the relation returns null. - A query that found nothing.
find()andfirst()return null when no row matches, soUser::where('email', $email)->first()->update([...])ends inCall to a member function update() on null. auth()->user()for a guest. On a route without theauthmiddleware, a visitor who is not logged in makesauth()->user()->nameread a property of null.- A missing value in decoded data. A key that is absent or null in a JSON payload, read with
?? nulland 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
$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
$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
$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
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
$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 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.
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.