From 23d75ebba8717f1f182531038bcae0887d06968a Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Thu, 16 Feb 2023 09:39:22 +0800 Subject: [PATCH] store-product-spus --- .../Api/Http/Controllers/StoreController.php | 21 +++++++++--------- .../Api/Http/Resources/ProductSpuResource.php | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 app/Endpoint/Api/Http/Resources/ProductSpuResource.php diff --git a/app/Endpoint/Api/Http/Controllers/StoreController.php b/app/Endpoint/Api/Http/Controllers/StoreController.php index f511c14b..c67b4661 100644 --- a/app/Endpoint/Api/Http/Controllers/StoreController.php +++ b/app/Endpoint/Api/Http/Controllers/StoreController.php @@ -7,7 +7,7 @@ use App\Models\Store\Store; use App\Models\{ProductSku, ProductSpu}; use App\Helpers\Paginator; use App\Models\Store\ProductSku as StoreProductSku; -use App\Endpoint\Api\Http\Resources\{StoreResource, StoreProductSkuResource, StoreProductSpuResource}; +use App\Endpoint\Api\Http\Resources\{StoreResource, StoreProductSkuResource, StoreProductSpuResource, ProductSpuResource}; use App\Endpoint\Api\Http\Resources\ProductFeatureResource; use App\Events\ProductSkuViewed; @@ -42,16 +42,17 @@ class StoreController extends Controller $input['sort'] = '-id'; } - // $spuIds = StoreProductSku::where('store_id', $store->id)->where('status', 1)->pluck('product_spu_id')->toArray(); - // $spuIds = array_unique($spuIds); - // $spuIds = array_values($spuIds); + $spuIds = StoreProductSku::where('store_id', $store->id)->where('status', 1)->pluck('product_spu_id')->toArray(); + $spuIds = array_unique($spuIds); + $spuIds = array_values($spuIds); + $list = ProductSpu::with(['specs'])->whereIn('id', $spuIds)->filter($input)->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50)); + return ProductSpuResource::collection($list); - $list = $store->productSpus() - ->with(['specs']) - ->filter($input) - ->wherePivot('status', 1) - ->distinct('product_spus.id') - ->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50)); + // $list = $store->productSpus() + // ->with(['specs']) + // ->filter($input) + // ->wherePivot('status', 1) + // ->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50)); return StoreProductSpuResource::collection($list); } diff --git a/app/Endpoint/Api/Http/Resources/ProductSpuResource.php b/app/Endpoint/Api/Http/Resources/ProductSpuResource.php new file mode 100644 index 00000000..011c63dd --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/ProductSpuResource.php @@ -0,0 +1,22 @@ + $this->id, + 'name' => $this->name, + 'cover' => (string) $this->cover, + 'sell_price' => (string) $this->sell_price_format, + 'vip_price' => (string) $this->vip_price_format, + 'market_price' => (string) $this->market_price_format, + 'stock' => (int) $this->stock, + 'specs' => $this->whenLoaded('specs'), + ]; + } +}