forked from eatvio/eatvio-infra
Merge pull request 'Добавить сидер теги для рецептов' (#30) from alex_chevski/eatvio:seederRecipe into main
Reviewed-on: eatvio/eatvio#30
This commit is contained in:
28
database/factories/TagFactory.php
Normal file
28
database/factories/TagFactory.php
Normal 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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -17,5 +17,6 @@ class DatabaseSeeder extends Seeder
|
||||
$this->call(RecipeSeeder::class);
|
||||
$this->call(RecipeAuthor::class);
|
||||
$this->call(RecipeCommentSeeder::class);
|
||||
$this->call(RecipeTagSeeder::class);
|
||||
}
|
||||
}
|
||||
|
||||
30
database/seeders/RecipeTagSeeder.php
Normal file
30
database/seeders/RecipeTagSeeder.php
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user