ldc-nature-and-respect/database/seeders/RecipeSeeder.php
Alexis Gousseau a333993456 add seeders
2023-03-16 09:47:16 +01:00

40 lines
1.2 KiB
PHP

<?php
namespace Database\Seeders;
use App\Lib\Helpers\FileStorageHelper;
use App\Models\Product;
use App\Models\Recipe;
use Illuminate\Database\Seeder;
class RecipeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$jj = 1;
Recipe::factory(35)->make()->each(function (Recipe $recipe) use (&$jj) {
$recipe->picture = FileStorageHelper::storeFile($recipe, new \SplFileInfo(\resource_path(
'../database/factories/assets/recipes/default-picture-1600x900.png'
)));
$recipe->picture_thumbnail = FileStorageHelper::storeFile($recipe, new \SplFileInfo(\resource_path(
'../database/factories/assets/recipes/default-picture-430x242.png'
)));
$recipe->order = $jj;
$recipe->saveOrFail();
$jj = $jj + 1;
});
// Seeder for recipes_products table.
$products = Product::query()->get();
Recipe::each(function (Recipe $model) use ($products) {
$offset = rand(0, 50);
$length = rand(0, 3);
$model->products()->saveMany($products->slice($offset, $length));
});
}
}