feat: enhance product detail retrieval to support variant selection and fallback image handling

This commit is contained in:
2026-07-01 01:05:31 +00:00
parent 1aa831f921
commit 46685a7141
5 changed files with 191 additions and 35 deletions

View File

@@ -2,11 +2,12 @@
namespace App\Domains\Catalog\Resources;
use App\Domains\Catalog\Models\ProductVariant;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin \App\Domains\Catalog\Models\ProductVariant
* @mixin ProductVariant
*/
class ProductVariantResource extends JsonResource
{
@@ -35,13 +36,19 @@ class ProductVariantResource extends JsonResource
->values();
}
// Fallback: use product-level attachments when the variant has none
$this->loadMissing('product.attachments');
if ($this->relationLoaded('fallbackAttachments')) {
return $this->fallbackAttachments
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
->values();
}
return $this->product?->attachments
?->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
?->values()
?? collect();
if ($this->relationLoaded('product') && $this->product?->relationLoaded('attachments')) {
return $this->product->attachments
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
->values();
}
return collect();
}),
];
}