My question is quite similar to this unanswered question.
I’m making a 2v2 ladder system in Laravel 11. I have 2 models:
- Users
- Games
There’s a pivot table between them with an added team_user
column, defining which team the user is on.
Users can have unlimited games, but a game has only 4 players, and their team in that game defined by aforementioned pivot column.
My seeder function looks like this currently:
$users = User::factory(20)->create();
Game::factory(100)
->hasAttached($users->random(2), [
'user_team' => 'team1'
])
->hasAttached($users->random(2), [
'user_team' => 'team2'
])
->create();
This kinda works. 20 users and 100 games are created, but the same 4 users are used for every game.
If I plug in the User::factory()
directly into hasAttached()
, it will generate new users for every single game.
What I want is 20 users randomly playing on teams in all 100 games (but also not end up with the same user multiple times in the same game).
I hope what I’m trying to do makes sense!
2
Answers
Why does end up with the same 4 users used for every game?
because Laravel factory runs the two calls of the hasAttach method just once and applies them on entire the collection not run them on each game
You can try this
I think you can run GameFactory in a loop with minimum code changes
This time on each cycle users will be picked randomly every time