budgetLimit; $end = $event->end; $set = $budgetLimit->limitrepetitions() ->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00')) ->where('enddate', $end->format('Y-m-d 00:00:00')) ->get(); if ($set->count() == 0) { $repetition = new LimitRepetition; $repetition->startdate = $budgetLimit->startdate; $repetition->enddate = $end; $repetition->amount = $budgetLimit->amount; $repetition->budgetLimit()->associate($budgetLimit); try { $repetition->save(); } catch (QueryException $e) { Log::error('Trying to save new LimitRepetition failed: ' . $e->getMessage()); } } if ($set->count() == 1) { $repetition = $set->first(); $repetition->amount = $budgetLimit->amount; $repetition->save(); } } /** * @param BudgetLimitUpdated $event */ public function update(BudgetLimitUpdated $event) { $budgetLimit = $event->budgetLimit; $end = $event->end; $set = $budgetLimit->limitrepetitions() ->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00')) ->where('enddate', $end->format('Y-m-d 00:00:00')) ->get(); if ($set->count() == 0) { $repetition = new LimitRepetition; $repetition->startdate = $budgetLimit->startdate; $repetition->enddate = $end; $repetition->amount = $budgetLimit->amount; $repetition->budgetLimit()->associate($budgetLimit); try { $repetition->save(); } catch (QueryException $e) { Log::error('Trying to save new LimitRepetition failed: ' . $e->getMessage()); } } if ($set->count() == 1) { $repetition = $set->first(); $repetition->amount = $budgetLimit->amount; $repetition->save(); } } }