From 06b9c919480d56922797d3f878c083f5bd3b68b5 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Thu, 20 Aug 2026 18:55:13 +0700 Subject: [PATCH] feat: enhance getVariants method to support multiple media URLs and conversions for product variants --- .../Admin/Master/Product/ProductService.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/Services/Admin/Master/Product/ProductService.php b/app/Services/Admin/Master/Product/ProductService.php index 0532fcb..8847ba5 100644 --- a/app/Services/Admin/Master/Product/ProductService.php +++ b/app/Services/Admin/Master/Product/ProductService.php @@ -81,15 +81,17 @@ public function getVariants(Product $product): Collection ->with(['productPrices:id,variant_id,type,price']) ->get() ->each(function (ProductVariant $variant) { - $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; + $allMedia = $variant->getMedia('images'); + $variant->photo_urls = $allMedia + ->map(fn ($m) => $this->s3Service->getTemporaryUrl($m->getPath())) + ->values() + ->toArray(); + $variant->photo_conversion_urls = $allMedia + ->map(fn ($m) => $m->getGeneratedConversions()->contains('thumb') + ? $this->s3Service->getTemporaryUrl($m->getPath('thumb')) + : $this->s3Service->getTemporaryUrl($m->getPath())) + ->values() + ->toArray(); $prices = $variant->productPrices->mapWithKeys(fn ($p) => [$p->type->value => $p->price]); $variant->prices = $prices;