belongsTo(User::class); } public function services() { return $this->hasMany(Service::class); } /** * Get the currency corresponding to the service. */ public function currency() { return $this->hasOne(Currency::class, 'code', 'currency_code'); } /** * Total of the order. * * @return float */ public function total(): Attribute { return Attribute::make( get: fn () => $this->services->sum(fn ($service) => $service->price * $service->quantity) ); } /** * Total of the order. * * @return string */ public function formattedTotal(): Attribute { return Attribute::make( get: fn () => new Price(['price' => $this->total, 'currency' => $this->currency]) ); } /** * Get all invoices for the order. */ public function invoices(): Attribute { // Each service has invoices (it is a hasManyThrough relationship order -> service -> invoiceItem -> invoice) $invoicesId = $this->services->map(fn ($service) => $service->invoiceItems->map(fn ($invoiceItem) => $invoiceItem->invoice_id))->flatten(); return new Attribute( get: fn () => Invoice::whereIn('id', $invoicesId) ); } }