AmountCast::class, 'amount_by_user' => AmountCast::class, ]; public function costUnit() : BelongsTo{ return $this->belongsTo(CostUnit::class); } public function invoiceType() : InvoiceType { return $this->belongsTo(InvoiceType::class, 'type', 'slug')->first(); } public function calculateAmount() : ?Amount { switch (true) { case $this->flat_amount !== null: return $this->flat_amount; default: $event = $this->costUnit()->first()->event()?->first(); if (null !== $event) { $participants = $event->participants()->count(); return $this->amount_by_user->multiply($participants); } else { dd('U'); return $this->amount_by_user; } } } }