37 lines
825 B
PHP
37 lines
825 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Database\Seeders\Classic;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\Recipe;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class RecipeSeeder extends Seeder
|
|
{
|
|
public static string $table = 'recipes';
|
|
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$products = Product::all();
|
|
|
|
if ($products->isEmpty()) {
|
|
$this->command->warn('Aucun produit trouvé. Exécutez le ProductSeeder en premier.');
|
|
|
|
return;
|
|
}
|
|
|
|
Recipe::factory()
|
|
->count(30)
|
|
->create()
|
|
->each(function (Recipe $recipe) use ($products) {
|
|
$recipe->products()->attach(
|
|
$products->random(random_int(1, 4))->pluck('id')->toArray()
|
|
);
|
|
});
|
|
}
|
|
}
|