transaction; $piggyBank = $repository->find($transaction['piggy_bank_id']); // valid piggy: if (is_null($piggyBank->id)) { return true; } $amount = strval($transaction['amount']); // piggy bank account something with amount: if ($transaction['source_account_id'] == $piggyBank->account_id) { // if the source of this transaction is the same as the piggy bank, // the money is being removed from the piggy bank. So the // amount must be negative: $amount = bcmul($amount, '-1'); } $repetition = $piggyBank->currentRelevantRep(); // add or remove the money from the piggy bank: $newAmount = bcadd(strval($repetition->currentamount), $amount); $repetition->currentamount = $newAmount; $repetition->save(); // now generate a piggy bank event: PiggyBankEvent::create(['piggy_bank_id' => $piggyBank->id, 'date' => $transaction['date'], 'amount' => $newAmount]); return true; } }