/** * @param Budget $budget * @param Carbon $date * @param $amount * * @return BudgetLimit */ public function updateLimitAmount(Budget $budget, Carbon $date, $amount) { // there should be a budget limit for this startdate: /** @var BudgetLimit $limit */ $limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->first(['budget_limits.*']); if (!$limit) { // if not, create one! $limit = new BudgetLimit(); $limit->budget()->associate($budget); $limit->startdate = $date; $limit->amount = $amount; $limit->repeat_freq = 'monthly'; $limit->repeats = 0; $limit->save(); // likewise, there should be a limit repetition to match the end date // (which is always the end of the month) but that is caught by an event. } else { if ($amount > 0) { $limit->amount = $amount; $limit->save(); } else { $limit->delete(); } } return $limit; }