6
0
Fork 0

store-product-spus

base
panliang 2023-02-16 09:39:22 +08:00
parent 9214b97f52
commit 23d75ebba8
2 changed files with 33 additions and 10 deletions

View File

@ -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);
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Endpoint\Api\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductSpuResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $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'),
];
}
}