php - Laravel Eloquent Modal - First Row? -
inside od seeder file tried first record of user-model. can't seem find correct way.
i want fetch first user, , id use in user_id
(foreign key).
this not working:
class terminalstableseeder extends seeder { /** * run database seeds. * * @return void */ public function run() { $user = user::findorfail(1)->first(); db::table('terminals')->delete(); terminal::create([ 'serial' => 'r-123-548-753', 'state' => 1, 'user_id' => $user->id ]); } }
it fails because said "1", requires 1 parameter, first row in user doesn't have id of 1, don't want hardcode this.
simple solution use firstorfail
$user = user::firstorfail(); //it return first record top terminal::create([ 'serial' => 'r-123-548-753', 'state' => 1, 'user_id' => $user->id' ])
Comments
Post a Comment