Home > Mobile >  Attempt to read property “client_id” on null using laravel
Attempt to read property “client_id” on null using laravel

Time:02-05

When i run db seed command on terminal but unfortunatly i get error Attempt to read property client_id on null please help me how can i resolve that ? thank u

Contractor model

    class Contractor extends Model
     {
            protected static function booted() {
                static::creating(function ($model) {
                     $model->client_id = auth()->user()->client_id;
                });
            }
     }

CodePudding user response:

There is not any authenticated user when you run the database seeders. so you have to make the user in your factory or authenticate the fake user in your database seeder

CodePudding user response:

During seeders, there's no auth()->user(). The most simple fix would be probably this:

$model->client_id = auth()->user()?->client_id;

It will work with PHP 8, from what I remember.

Or, if not, just add if (auth()->check()) before adding this line.

  • Related