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
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
During seeders, there’s no
auth()->user()
.The most simple fix would be probably this:
It will work with PHP 8, from what I remember.
Or, if not, just add
if (auth()->check())
before adding this line.