*/ public function toArray(Request $request): array { return [ 'id' => $this->id, 'class_package_unit_id' => $this->class_package_unit_id, 'type' => $this->type, 'register_value' => (float) $this->register_value, 'register_installments_min' => $this->register_installments_min, 'register_installments_max' => $this->register_installments_max, 'package_value' => (float) $this->package_value, 'package_installments_min' => $this->package_installments_min, 'package_installments_max' => $this->package_installments_max, 'discount_value' => $this->discount_value !== null ? (float) $this->discount_value : null, 'material_value' => (float) $this->material_value, 'material_installments_min' => $this->material_installments_min, 'material_installments_max' => $this->material_installments_max, 'total_value' => $this->totalValue(), 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'), 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'), // Relationships 'materials' => $this->whenLoaded('products', fn () => $this->products->map(fn ($material) => [ 'product_id' => $material->product_id, 'name' => $material->product?->name, 'quantity' => $material->quantity, 'price' => (float) $material->price, ])->values() ), ]; } /** * @param \Illuminate\Database\Eloquent\Collection $resource * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection */ public static function collection($resource): AnonymousResourceCollection { return parent::collection($resource); } }