Skip to content

Commit 85af461

Browse files
feat: add food images
1 parent 87d7226 commit 85af461

26 files changed

Lines changed: 37 additions & 5 deletions

app/Models/Food.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function getSlugOptions(): SlugOptions
2323
->saveSlugsTo('slug');
2424
}
2525

26+
protected $appends = ['image_url'];
27+
2628
protected $fillable = [
2729
'food_category_id',
2830
'name',

database/seeders/FoodAndRecipeSeeder.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Models\Food;
99
use App\Models\FoodAddonGroup;
1010
use App\Models\FoodCategory;
11+
use App\Models\FoodImage;
1112
use App\Models\FoodOutlet;
1213
use App\Models\FoodRecipe;
1314
use App\Models\FoodVariant;
@@ -16,6 +17,7 @@
1617
use App\Models\Outlet;
1718
use App\Models\Unit;
1819
use Illuminate\Database\Seeder;
20+
use Illuminate\Support\Facades\Storage;
1921
use Illuminate\Support\Str;
2022

2123
class FoodAndRecipeSeeder extends Seeder
@@ -459,6 +461,32 @@ class FoodAndRecipeSeeder extends Seeder
459461

460462
// ─────────────────────────────────────────────────────────────────────────
461463

464+
private function seedFoodImage(Food $food, string $sku): void
465+
{
466+
if ($food->images()->exists()) {
467+
return;
468+
}
469+
470+
$matches = glob(public_path("assets/foods/{$sku}_*.png"));
471+
472+
if (empty($matches)) {
473+
return;
474+
}
475+
476+
$sourcePath = $matches[0];
477+
$destination = 'food-images/' . basename($sourcePath);
478+
Storage::disk('public')->put($destination, file_get_contents($sourcePath));
479+
480+
FoodImage::create([
481+
'food_id' => $food->id,
482+
'image' => $destination,
483+
'is_primary' => true,
484+
'sort_order' => 1,
485+
]);
486+
487+
$food->update(['image' => $destination]);
488+
}
489+
462490
public function run(): void
463491
{
464492
$outlets = Outlet::pluck('id')->all();
@@ -506,6 +534,8 @@ public function run(): void
506534
['is_available' => true, 'is_active' => true]
507535
);
508536
}
537+
538+
$this->seedFoodImage($food, $sku);
509539
}
510540

511541
// 3. Variants + outlet assignments
1.86 MB
Loading
1.69 MB
Loading
1.89 MB
Loading
2 MB
Loading
2.07 MB
Loading
2.31 MB
Loading
2.5 MB
Loading
2.42 MB
Loading

0 commit comments

Comments
 (0)