skip to Main Content

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;
                });
            }
     }

2

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search