Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel says 'Attempt to read property "name" on null' in a view or controller

ErrorAttempt to read property "name" on null

Short 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.

  • Applies to PHP 8.3, Laravel 11, Laravel 12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);"
  2. 2Match what you see, then jump to its fix

    1. 1The line reads $model->relation->property and the foreign key column is null or points to a deleted rowA belongsTo relation has no related rowFix
    2. 2A chain such as $order->customer->address->city fails at one linkThe code assumes a value that can legitimately be nullFix

What does the error look like?

  • Attempt to read property "name" on null
  • Attempt 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

Did this fix it?

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 customer

Confirmed by php.net

Did this fix it?

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 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.