Replace this
$round = Round::factory()->create(['user' => $user,]);
with this
$round = Round::factory()->create(['user_id' => $user->id]);
Summer Sale! All accounts are 50% off this week.
Trying to pass a variable from DatabaseSeeder to factory in Laravel 8; is there new syntax?
I need to loop thru my users with foreach. I see a bunch of examples of passing variable to a factory as an array like this
// DatabaseSeeder.php
foreach ($users as $user) {
$round = Round::factory()->create(['user' => $user,]);
}
and I've also seen this:
$round = Round::factory()->create($user);
Most of those examples are several years old, but one is Sep'19. But neither way is working for me. I get:
// Terminal: php artisan db:seed
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: user", …
My foreach loop is definitely producing valid $user (confirmed via dd($user)). My factory def looks like this:
// RoundFactory.php
return [
'user_id' => $user,
'other_field' => $this->faker-> …something,
other fields …
]
So is there some other way to achieve this? Thanks.
Please or to participate in this conversation.