'Attempt to assign property on null' after moving to PHP 8
Attempt to assign property "status" on nullShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
The 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
The failing line writes to something like $post->user->name, and some posts have no userA relation has no related rowFix - 3
The same code ran on an older server without any errorCode written for PHP 7 relied on auto-created objectsFix
- 1
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/shipConfirmed by raw.githubusercontent.com
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 tinkerConfirmed by raw.githubusercontent.com
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 202Fix
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 -vConfirmed by php.net
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 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