6
0
Fork 0

优化购物车商品数据结构

release
李静 2021-12-15 20:00:46 +08:00
parent 166b260d2c
commit d8e5ac0b5e
1 changed files with 15 additions and 19 deletions

View File

@ -2,6 +2,7 @@
namespace App\Endpoint\Api\Http\Resources; namespace App\Endpoint\Api\Http\Resources;
use App\Endpoint\Api\Http\Resources\ProductSku\ProductSkuSimpleResource;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
class ShoppingCartItemResource extends JsonResource class ShoppingCartItemResource extends JsonResource
@ -16,27 +17,22 @@ class ShoppingCartItemResource extends JsonResource
{ {
return [ return [
'id' => $this->id, 'id' => $this->id,
'sku' => $this->when($this->relationLoaded('sku') && $this->sku, function () { 'sku' => $this->when($this->relationLoaded('sku'), function () {
if ($this->sku) {
return ProductSkuSimpleResource::make($this->sku);
}
return [ return [
'id' => $this->sku->id, 'id' => $this->id,
'name' => $this->sku->name, 'name' => $this->name,
'cover' => (string) $this->sku->cover, 'cover' => (string) $this->cover,
'sell_price' => $this->sku->sell_price, 'sell_price' => $this->sell_price,
'vip_price' => (string) $this->sku->vip_price, 'vip_price' => (string) $this->vip_price,
'specs' => array_values((array) $this->sku->specs), 'specs' => array_values((array) $this->specs),
'stock' => (int) $this->sku->saleable_stock, 'stock' => 0,
'is_online' => $this->sku->isOnline(), 'is_online' => false,
]; ];
}, [ }),
'id' => $this->id,
'name' => $this->name,
'cover' => (string) $this->cover,
'sell_price' => $this->sell_price,
'vip_price' => (string) $this->vip_price,
'specs' => array_values((array) $this->specs),
'stock' => 0,
'is_online' => false,
]),
'quantity' => $this->quantity, 'quantity' => $this->quantity,
]; ];
} }