ldc-legaulois/database/seeders/Development/RecipeSeeder.php

45 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2024-05-15 12:40:31 +02:00
<?php
namespace Database\Seeders\Development;
use App\Lib\Helpers\FileStorageHelper;
2024-05-21 17:00:46 +02:00
use App\Models\Product;
2024-05-15 12:40:31 +02:00
use App\Models\Recipe;
use Illuminate\Database\Seeder;
2024-05-21 17:00:46 +02:00
use Illuminate\Support\Facades\DB;
2024-05-15 12:40:31 +02:00
class RecipeSeeder extends Seeder
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Recipe::class;
2024-05-15 12:40:31 +02:00
/**
* Run the database seeds.
*/
public function run(): void
{
Recipe::factory(20)->make()->each(function (Recipe $recipe, int $index) {
$recipe->picture = FileStorageHelper::storeFile(
$recipe,
new \SplFileInfo(\resource_path(
'../database/factories/assets/recipes/default-picture-720x405.png'
))
);
$recipe->highlight = ($index === 0) ? true : false;
$recipe->saveQuietly();
2024-05-21 17:00:46 +02:00
DB::table('productables')->insert(
[
'product_id' => Product::inRandomOrder()->first()->getKey(),
'productable_id' => $recipe->getKey(),
2026-09-25 16:25:31 +02:00
'productable_type' => 'App\Models\Recipe',
2024-05-21 17:00:46 +02:00
]
);
2024-05-15 12:40:31 +02:00
});
}
}