Maaz Zindani

Loading the index…

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

'Attempt to assign property on null' after moving to PHP 8

ErrorAttempt to assign property "status" on null

Short answer

PHP 8 turned a write to a property of null into an Error, where older PHP created an object silently. The variable you assign to is null, usually because find() or first() matched no row or a relation has no related row. Use findOrFail() or firstOrFail(), or define a default model on the relation.

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

Fix it fast

  1. 1Find out which cause you have

    Read the line from the error and find which variable is on the left of the arrow. Then dump it right before that line to see what produced null.

    // just before the failing line
    dd($order);
  2. 2Match what you see, then jump to its fix

    1. 1The variable comes from find(), first() or firstWhere(), and the id or condition is wrong for the dataA lookup found no row and returned nullFix
    2. 2The failing line writes to something like $post->user->name, and some posts have no userA relation has no related rowFix
    3. 3The same code ran on an older server without any errorCode written for PHP 7 relied on auto-created objectsFix

What does the error look like?

  • Attempt to assign property "status" on null

Cause 1 of 3

A lookup found no row and returned null

1Check

Laravel's docs say findOrFail() and firstOrFail() throw ModelNotFoundException when no result is found, which is the safe alternative. The PHP 8 migration guide states that writing to a property of a non-object now throws an Error.

php artisan tinker --execute="var_dump(App\Models\Order::find(999999));"

2Fix

Use findOrFail() or firstOrFail(): Laravel throws ModelNotFoundException and sends a 404 if you do not catch it.

$order = Order::findOrFail($id);
$order->status = 'shipped';
$order->save();

3Verify

Call the route with an id that does not exist and expect a 404 and not a 500.

curl -i http://localhost:8000/orders/999999/ship

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

A relation has no related row

1Check

Laravel's docs describe withDefault() as returning a default model when the relationship is null, which covers a missing related row.

php artisan tinker --execute="var_dump(App\Models\Post::doesntHave('user')->count());"

2Fix

Define the relation with withDefault(), so the property exists, or guard the write with an explicit check.

public function user()
{
    return $this->belongsTo(User::class)->withDefault();
}

3Verify

Run the code on a row that has no user and confirm it no longer throws.

php artisan tinker

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

Code written for PHP 7 relied on auto-created objects

1Check

The PHP 8.0 migration notes list writing to a property of a non-object, which used to create an stdClass for null, as one of the warnings now turned into an Error.

grep -rn "->[a-z_]* = " app/ | head -n 20

2Fix

Create the object on purpose before writing to it, or use an array.

$data = new stdClass();
$data->status = 'shipped';

3Verify

Re-run the code path on PHP 8.

php -v

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Use findOrFail() in controllers, so a missing row becomes a 404 and not a crash.
  • Run static analysis (PHPStan or Larastan) in CI, which reports possibly-null access.
  • Cover the 'record does not exist' path with a feature test.

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.