I got this error while running
php artisan migrate:fresh --seed
this command will create table in MySQL database and fill the database details DB_DATABASE in .env file.
parent::boot();
static::creating(function($model)
{
$user = Auth::user();
model->created_by = $user->id ? $user->id : 1 ;
});
static::updating(function($model)
{
$user = Auth::user();```
Controller:
2
Answers
The issue here is that the value of
$user
isnull
andnull
doesn’t have any properties.$user
will always benull
with your code asAuth::user()
will benull
. You have no authenticatedUser
during the execution of your seeder.If you want to assign a
User
to your$model
and you have seeded yourUser
table, you could get aUser
that way.If you don’t want a particular
User
then you could do:Change this line:
to this:
You must first check if the
$user
is empty or not.