forked from eatvio/eatvio-infra
Merge pull request 'recipe_seeders' (#15) from recipe_seeders into main
Reviewed-on: eatvio/eatvio#15
This commit is contained in:
@@ -7,7 +7,7 @@ use Illuminate\Database\Seeder;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\RecipeAuthor;
|
||||
use App\Models\User;
|
||||
use Faker\Factory as FakerFaktory;
|
||||
use Faker\Factory as FakerFactory;
|
||||
use DB;
|
||||
|
||||
class RecipeSeeder extends Seeder
|
||||
@@ -19,10 +19,11 @@ class RecipeSeeder extends Seeder
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$faker = FakerFaktory::create();
|
||||
$faker = FakerFactory::create();
|
||||
$images = [];
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$recipe = new Recipe();
|
||||
$recipe->image = $faker->imageUrl(640, 480, 'food');
|
||||
$recipe->image = $this->downloadRandomImage();
|
||||
$recipe->state = 'active';
|
||||
$recipe->type = $faker->randomElement(['first_meals', 'second_meals', 'salads', 'snacks', 'drinks', 'other']);
|
||||
$recipe->title = $faker->sentence(5);
|
||||
@@ -30,9 +31,9 @@ class RecipeSeeder extends Seeder
|
||||
$recipe->cooking_time = $faker->randomElement([10,20,30,60,120,14400]);
|
||||
$recipe->description = $faker->text(200);
|
||||
$recipe->steps = [
|
||||
['image' => $faker->imageUrl(640, 480, 'food'), 'description' => $faker->text(100)],
|
||||
['image' => $faker->imageUrl(640, 480, 'food'), 'description' => $faker->text(100)],
|
||||
['image' => $faker->imageUrl(640, 480, 'food'), 'description' => $faker->text(100)]
|
||||
['image' => $this->downloadRandomImage(), 'description' => $faker->text(100)],
|
||||
['image' => $this->downloadRandomImage(), 'description' => $faker->text(100)],
|
||||
['image' => $this->downloadRandomImage(), 'description' => $faker->text(100)]
|
||||
];
|
||||
$recipe->ingredients = [
|
||||
['name' => $faker->word, 'portion' => $faker->numberBetween(1, 20), 'unit' => $faker->randomElement(['teespoon', 'tablespoon', 'ml', 'pcs', 'clove', 'to_taste'])],
|
||||
@@ -42,5 +43,21 @@ class RecipeSeeder extends Seeder
|
||||
$recipe->save();
|
||||
User::inRandomOrder()->first()->author_of_recipe()->attach($recipe->id);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public function downloadRandomImage(): string
|
||||
{
|
||||
$faker = FakerFactory::create();
|
||||
$url = $faker->imageUrl(640, 480, 'food', true);
|
||||
$contents = file_get_contents($url);
|
||||
$dir_path = storage_path('app/public/recipe');
|
||||
$path = 'recipe/' . uniqid() . '.jpg';
|
||||
if (!file_exists( $dir_path)) {
|
||||
mkdir( $dir_path, 0777, true);
|
||||
}
|
||||
file_put_contents(storage_path('app/public/' . $path), $contents);
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user