app/database/seeders/DishesSeeder.php

44 lines
1.3 KiB
PHP
Raw Normal View History

2025-10-13 14:57:11 +02:00
<?php
namespace Database\Seeders;
use App\Models\Dish;
use App\Models\Planner;
use App\Models\User;
use Illuminate\Database\Seeder;
class DishesSeeder extends Seeder
{
public function run(): void
{
2026-01-04 03:30:06 +01:00
$planner = Planner::first() ?? Planner::factory()->create();
2025-10-13 14:57:11 +02:00
2026-01-04 03:30:06 +01:00
// 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
2025-10-13 14:57:11 +02:00
collect([
2026-01-04 03:30:06 +01:00
'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([
2025-10-13 14:57:11 +02:00
'planner_id' => $planner->id,
'name' => $name,
2026-01-04 03:30:06 +01:00
]);
$dish->users()->attach($userOptions[array_rand($userOptions)]);
});
2025-10-13 14:57:11 +02:00
}
}