*/ public function toArray(Request $request): array { $variant = $this->variant; $product = $variant?->product; $attributesText = ''; if ($variant && $variant->relationLoaded('definitions')) { $attributesText = $variant->definitions ->map(function ($definition) { $attrName = $definition->productAttribute?->attribute?->nombre; $value = $definition->value; return $attrName ? "{$attrName}: {$value}" : $value; }) ->filter() ->implode(', '); } $productName = $product?->nombre; if ($productName && $attributesText !== '') { $productName .= " ({$attributesText})"; } $imageUrl = null; if ($variant && $variant->relationLoaded('attachments')) { $firstAttachment = $variant->attachments->first(); if ($firstAttachment) { $imageUrl = $firstAttachment->getTemporaryUrl(1440); } } return [ 'id' => $this->id, 'cantidad' => $this->cantidad, 'precio_unitario' => $this->formatMoney($product?->precio), 'product_id' => $product?->id, 'product_variant_id' => $this->producto_variante_id, 'product' => $product === null ? null : [ 'nombre' => $productName, 'imagen' => $imageUrl, ], ]; } protected function formatMoney(float|int|string|null $amount): string { return number_format((float) ($amount ?? 0), 2, '.', ''); } }