image = $this->downloadRandomImage(); $recipe->state = 'active'; $recipe->type = $faker->randomElement(['first_meals', 'second_meals', 'salads', 'snacks', 'drinks', 'other']); $recipe->title = $faker->sentence(5); $recipe->video_url = $faker->url; $recipe->cooking_time = $faker->randomElement([10,20,30,60,120,14400]); $recipe->description = $faker->text(200); $recipe->steps = [ ['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'])], ['name' => $faker->word, 'portion' => $faker->numberBetween(1, 20), 'unit' => $faker->randomElement(['teespoon', 'tablespoon', 'ml', 'pcs', 'clove', 'to_taste'])], ['name' => $faker->word, 'portion' => $faker->numberBetween(1, 20), 'unit' => $faker->randomElement(['teespoon', 'tablespoon', 'ml', 'pcs', 'clove', 'to_taste'])], ]; $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; } }