*/ public function toArray(Request $request): array { $variant = $this->variant; $product = $variant?->product; return [ 'id' => $this->id, 'cantidad' => $this->cantidad, 'precio_unitario' => $this->formatMoney($this->precio_unitario), 'total' => $this->formatMoney($this->total), 'product' => $product === null ? null : [ 'id' => $product->id, 'nombre' => $product->nombre, 'slug' => $product->slug, ], 'variant' => $variant === null ? null : [ 'id' => $variant->id, 'definitions' => ProductVariantDefinitionResource::collection($variant->definitions), ], ]; } protected function formatMoney(float|int|string|null $amount): ?string { if ($amount === null) { return null; } return number_format((float) $amount, 2, '.', ''); } }