From b782272d9832e129f07f14febb53171b62f08376 Mon Sep 17 00:00:00 2001 From: maxxkim Date: Fri, 15 Dec 2023 10:47:19 +0300 Subject: [PATCH 1/3] Seeder images --- database/seeders/RecipeSeeder.php | 25 ++++++++++++++----- .../views/livewire/recipe/list-view.blade.php | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php index b92f020..c4fb37f 100644 --- a/database/seeders/RecipeSeeder.php +++ b/database/seeders/RecipeSeeder.php @@ -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,17 @@ 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); + $path = 'recipe/' . uniqid() . '.jpg'; + file_put_contents(storage_path('app/public/' . $path), $contents); + + return $path; } } diff --git a/resources/views/livewire/recipe/list-view.blade.php b/resources/views/livewire/recipe/list-view.blade.php index e6e5a37..9deff42 100644 --- a/resources/views/livewire/recipe/list-view.blade.php +++ b/resources/views/livewire/recipe/list-view.blade.php @@ -27,7 +27,7 @@ @if(is_null($image)) formats @else - recipe + recipe @endif From 8b417a9ff5b0b6108bb0b82d4f8b954f3e912657 Mon Sep 17 00:00:00 2001 From: maxxkim Date: Fri, 15 Dec 2023 10:48:36 +0300 Subject: [PATCH 2/3] Seeder minor fix --- resources/views/livewire/recipe/list-view.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/livewire/recipe/list-view.blade.php b/resources/views/livewire/recipe/list-view.blade.php index 9deff42..e6e5a37 100644 --- a/resources/views/livewire/recipe/list-view.blade.php +++ b/resources/views/livewire/recipe/list-view.blade.php @@ -27,7 +27,7 @@ @if(is_null($image)) formats @else - recipe + recipe @endif From 14e0f36e7646f5b115a861a41b1034031d71cf2b Mon Sep 17 00:00:00 2001 From: maxxkim Date: Fri, 15 Dec 2023 11:56:03 +0300 Subject: [PATCH 3/3] Path exists validation --- database/seeders/RecipeSeeder.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php index c4fb37f..939c789 100644 --- a/database/seeders/RecipeSeeder.php +++ b/database/seeders/RecipeSeeder.php @@ -51,7 +51,11 @@ class RecipeSeeder extends Seeder $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;