*/ public function toArray(): array { return [ 'id' => $this->id, 'itemsCount' => $this->itemsCount, 'totalPrice' => $this->totalPrice, 'items' => $this->items, ]; } public static function fromModel(Cart $cart) { return new self( id: $cart->id, itemsCount: $cart->products->count(), totalPrice: $cart->products->sum(fn ($product) => $product->pivot->price * $product->pivot->quantity), items: $cart->products->map(fn ($product) => new CartItemDTO( id: $product->id, title: $product->title, quantity: $product->pivot->quantity, price: $product->actual_price, subtotal: $product->actual_price * $product->pivot->quantity, image: $product->images->first()->path, ))->toArray() ); } }