Merge pull request 'Добавить сидер теги для рецептов' (#30) from alex_chevski/eatvio:seederRecipe into main

Reviewed-on: eatvio/eatvio#30
This commit is contained in:
apoloz
2023-12-25 11:30:27 +00:00
3 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use FakerRestaurant\Provider\en_US\Restaurant;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Tag>
*/
class TagFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$faker = \Faker\Factory::create();
$this->faker->addProvider(new Restaurant($faker));
return [
'name' => $this->faker->foodName(),
];
}
}

View File

@@ -17,5 +17,6 @@ class DatabaseSeeder extends Seeder
$this->call(RecipeSeeder::class);
$this->call(RecipeAuthor::class);
$this->call(RecipeCommentSeeder::class);
$this->call(RecipeTagSeeder::class);
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Models\Recipe;
use Illuminate\Database\Seeder;
use App\Models\Tag;
class RecipeTagSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
if (Recipe::count() > 0) {
$recipes = Recipe::all();
$recipes->each(static function ($recipe): void {
$tags = Tag::factory()->count(5)->create();
$tagsIds = collect($tags)->map(fn($curTag) => $curTag['id']);
$recipe->tags()->attach($tagsIds);
});
}
}
}