From 9cca0427c84ec6b715820644643f68b47ed72f62 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Thu, 20 Aug 2026 11:28:28 +0700 Subject: [PATCH] feat: simplify media handling in getVariants method and enhance price mapping for product variants --- .../Admin/Master/Product/ProductService.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/Services/Admin/Master/Product/ProductService.php b/app/Services/Admin/Master/Product/ProductService.php index e1136f6..0532fcb 100644 --- a/app/Services/Admin/Master/Product/ProductService.php +++ b/app/Services/Admin/Master/Product/ProductService.php @@ -81,13 +81,18 @@ public function getVariants(Product $product): Collection ->with(['productPrices:id,variant_id,type,price']) ->get() ->each(function (ProductVariant $variant) { - $media = $variant->getMedia('images'); - $variant->photo_urls = $media->map(fn ($m) => $this->s3Service->getTemporaryUrl($m->getPath()))->toArray(); - $variant->photo_conversion_urls = $media->map( - fn ($m) => $m->getGeneratedConversions()->contains('thumb') - ? $this->s3Service->getTemporaryUrl($m->getPath('thumb')) - : $this->s3Service->getTemporaryUrl($m->getPath()) - )->toArray(); + $media = $variant->getFirstMedia('images'); + $variant->photo_url = $media + ? $this->s3Service->getTemporaryUrl($media->getPath()) + : null; + $variant->photo_conversion_url = $media + ? ($media->getGeneratedConversions()->contains('thumb') + ? $this->s3Service->getTemporaryUrl($media->getPath('thumb')) + : $this->s3Service->getTemporaryUrl($media->getPath())) + : null; + + $prices = $variant->productPrices->mapWithKeys(fn ($p) => [$p->type->value => $p->price]); + $variant->prices = $prices; }); }