Fix seeder

This commit is contained in:
myrmidex 2026-01-04 03:30:06 +01:00
parent 01648359b5
commit 6723c9813b

View file

@ -11,23 +11,33 @@ class DishesSeeder extends Seeder
{
public function run(): void
{
$users = User::all();
$userOptions = collect([
[$users->first()],
[$users->last()],
[$users->first(), $users->last()],
]);
$planner = Planner::first() ?? Planner::factory()->create();
$planner = Planner::all()->first() ?? Planner::factory()->create();
// Get users belonging to this planner
$users = User::where('planner_id', $planner->id)->get();
if ($users->isEmpty()) {
$this->command->warn('No users found for planner. Skipping dishes seeder.');
return;
}
$userIds = $users->pluck('id')->toArray();
// Build possible user combinations (individual users + all users together)
$userOptions = collect($userIds)->map(fn ($id) => [$id])->toArray();
$userOptions[] = $userIds; // all users
collect([
'lasagne', 'pizza', 'burger', 'fries', 'salad', 'sushi', 'pancakes', 'ice cream', 'spaghetti', 'mac and cheese',
'steak', 'chicken', 'beef', 'pork', 'fish', 'chips', 'cake',
])->map(fn (string $name) => Dish::factory()
->create([
'Lasagne', 'Pizza', 'Burger', 'Fries', 'Salad', 'Sushi', 'Pancakes', 'Ice Cream', 'Spaghetti', 'Mac and Cheese',
'Steak', 'Chicken', 'Beef', 'Pork', 'Fish', 'Chips', 'Cake',
])->each(function (string $name) use ($planner, $userOptions) {
$dish = Dish::factory()->create([
'planner_id' => $planner->id,
'name' => $name,
])
)->each(fn (Dish $dish) => $dish->users()->attach($userOptions->random()));
]);
$dish->users()->attach($userOptions[array_rand($userOptions)]);
});
}
}