Laravel says 'Attempt to read property "name" on null' in a view or controller
Attempt to read property "name" on nullShort answer
PHP raises this warning when you read a property on a value that is null. In Laravel that is almost always a relationship with no related row. Decide what a missing record should show: use withDefault on the relation, read it with the nullsafe operator, or fix the data that left the foreign key empty.
Fix it fast
1Find out which cause you have
Open the file and line from the error. Dump the value in front of the property: if it is null, the relation or variable has no model behind it.
php artisan tinker --execute="dump(App\Models\Post::first()->user);"2Match what you see, then jump to its fix
What does the error look like?
Attempt to read property "name" on nullAttempt to read property "id" on null
Cause 1 of 2
A belongsTo relation has no related row
1Check
Find the records whose foreign key is empty (adjust the column name).
php artisan tinker --execute="App\Models\Post::whereNull('user_id')->count();"2Fix
If a missing record is allowed, return a default model from the relation. Laravel documents withDefault for belongsTo, hasOne, hasOneThrough and morphOne.
public function user(): BelongsTo
{
return $this->belongsTo(User::class)->withDefault([
'name' => 'Guest Author',
]);
}3Verify
Load a post that has no user; the view shows the default name and no error.
php artisan tinker --execute="echo App\Models\Post::whereNull('user_id')->first()->user->name;"Confirmed by laravel.com
Cause 2 of 2
The code assumes a value that can legitimately be null
1Check
Work out which link in the chain can be null in real data.
php artisan tinker --execute="dump(App\Models\Order::whereNull('customer_id')->count());"2Fix
PHP 8 has the nullsafe operator: the chain returns null instead of throwing when a link is null. The manual says to use it when null is an expected value, and to throw an exception when it signals an error.
$city = $order->customer?->address?->city;
// in Blade
{{ $order->customer?->address?->city ?? 'No address' }}3Verify
Load an order with no customer; the page shows the fallback text.
# open the page for a record that has no customerConfirmed by php.net
How do I stop it coming back?
- Add a foreign key constraint so a relation cannot point to a missing row.
- Use withDefault where a missing parent is normal, and nullsafe access in views.
- Decide in the code review what each nullable relation should show when it is empty.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix