'decimal:2', 'unit_price' => 'decimal:2', 'total' => 'decimal:2', 'sort_order' => 'integer', ]; /** * Get the invoice that owns the item. */ public function invoice(): BelongsTo { return $this->belongsTo(Invoice::class); } /** * Boot the model. */ protected static function boot() { parent::boot(); static::saving(function ($item) { $item->total = $item->quantity * $item->unit_price; }); static::saved(function ($item) { $item->invoice->calculateTotals(); }); static::deleted(function ($item) { $item->invoice->calculateTotals(); }); } }