From 58a9aa685b1e79c2e9133ac733e4c755fcf68fc6 Mon Sep 17 00:00:00 2001 From: pbao9 Date: Wed, 11 Feb 2026 11:52:43 +0700 Subject: [PATCH] feat: Add active pricing details to service list resource and eager load only active pricings in the service repository. --- app/Http/Resources/Api/Service/ServiceListResource.php | 6 ++++++ app/Repositories/Api/Service/ServiceRepository.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Http/Resources/Api/Service/ServiceListResource.php b/app/Http/Resources/Api/Service/ServiceListResource.php index bc4e529..8186f9e 100644 --- a/app/Http/Resources/Api/Service/ServiceListResource.php +++ b/app/Http/Resources/Api/Service/ServiceListResource.php @@ -32,6 +32,12 @@ class ServiceListResource extends JsonResource 'stock' => $this->stock, 'price' => $this->price, 'price_sale' => $this->price_sale, + 'pricing' => $this->pricings->mapWithKeys(function ($pricing) { + return [$pricing->billing_cycle => $pricing->price]; + }), + 'pricing_sale' => $this->pricings->mapWithKeys(function ($pricing) { + return [$pricing->billing_cycle => $pricing->price_sale]; + }), 'group' => $this->category->name, 'os' => $this->osVersions->groupBy('os_group_id')->map(function ($versions) { $group = $versions->first()->osGroup; diff --git a/app/Repositories/Api/Service/ServiceRepository.php b/app/Repositories/Api/Service/ServiceRepository.php index 8191920..81df2ef 100644 --- a/app/Repositories/Api/Service/ServiceRepository.php +++ b/app/Repositories/Api/Service/ServiceRepository.php @@ -18,7 +18,7 @@ class ServiceRepository extends BaseApiRepository implements ServiceRepositoryIn public function getActiveServices(int $limit = 10) { return $this->model - ->with(['osVersions.osGroup', 'category']) + ->with(['osVersions.osGroup', 'category', 'pricings' => fn($q) => $q->where('is_active', 1)]) ->where('is_active', 1) ->orderBy('sort_order', 'asc') ->paginate($limit);