public function getBalanceByType($typeId) { $type = CreditType::find($typeId); $transactions = $this->model->transactions()->where('credit_type_id', '=', $type->id)->sum('amount'); $credits = $this->getTotalCreditByType($type->id); return $credits - $transactions; }
public function chargeCredits($amount, $typeId) { // Check if the type of credit exists $type = CreditType::find($typeId); if (!$type) { return false; } // check if the Model has sufficient balance to trade if ($this->getBalanceByType($type->slug) < $amount) { throw new InsufficientFundsException($this, $this->id, $this->getBalanceByType($type->id) - $amount); } // All fine, take the cash $transaction = (new Transaction())->fill(['amount' => $amount, 'credit_type_id' => $type->id]); $this->transactions()->save($transaction); return $transaction; }