belongsToMany(Recipe::class, 'recipes_spices') ->withPivot('amount_in_percent') ->withTimestamps(); } public function stockMovements() { return $this->hasMany(SpiceStockMovement::class); } public function recalculateQuantity(): void { $this->update(['quantity' => $this->stockMovements()->sum('amount')]); } }