Fix seeder
This commit is contained in:
parent
01648359b5
commit
6723c9813b
1 changed files with 23 additions and 13 deletions
|
|
@ -11,23 +11,33 @@ class DishesSeeder extends Seeder
|
||||||
{
|
{
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
$users = User::all();
|
$planner = Planner::first() ?? Planner::factory()->create();
|
||||||
$userOptions = collect([
|
|
||||||
[$users->first()],
|
|
||||||
[$users->last()],
|
|
||||||
[$users->first(), $users->last()],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$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([
|
collect([
|
||||||
'lasagne', 'pizza', 'burger', 'fries', 'salad', 'sushi', 'pancakes', 'ice cream', 'spaghetti', 'mac and cheese',
|
'Lasagne', 'Pizza', 'Burger', 'Fries', 'Salad', 'Sushi', 'Pancakes', 'Ice Cream', 'Spaghetti', 'Mac and Cheese',
|
||||||
'steak', 'chicken', 'beef', 'pork', 'fish', 'chips', 'cake',
|
'Steak', 'Chicken', 'Beef', 'Pork', 'Fish', 'Chips', 'Cake',
|
||||||
])->map(fn (string $name) => Dish::factory()
|
])->each(function (string $name) use ($planner, $userOptions) {
|
||||||
->create([
|
$dish = Dish::factory()->create([
|
||||||
'planner_id' => $planner->id,
|
'planner_id' => $planner->id,
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
])
|
]);
|
||||||
)->each(fn (Dish $dish) => $dish->users()->attach($userOptions->random()));
|
|
||||||
|
$dish->users()->attach($userOptions[array_rand($userOptions)]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue