created_by=$user->id?$user->id">
This error occurs when runningphp artisan migrate:fresh --seedThis 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
$useris empty.The problem here is that
$userhas a value ofnullandnulldoes not have any attributes.$userwill always benullwhereas your codeAuth::user()will benull. You did not have an authenticateduserduring the seeding process.If you want to assign
Userto your$modeland you have seeded theUsertable, you can get aUser like this.$model->created_by = \App\Models\User::where('id', 5)->first();If you don't want a specific
userthen you can do this: