This error occurs when running
php artisan migrate:fresh --seed
This command will create a table in the MySQL database and populate the .env file with database details DB_DATABASE.
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:
Change this line:
Regarding:
You must first check if
$user
is empty.The problem here is that
$user
has a value ofnull
andnull
does not have any attributes.$user
will always benull
whereas your codeAuth::user()
will benull
. You did not have an authenticateduser
during the seeding process.If you want to assign
User
to your$model
and you have seeded theUser
table, you can get aUser like this
.If you don't want a specific
user
then you can do this: