From ab66d45671fe964e868675a9d46a46bcfe8731bd Mon Sep 17 00:00:00 2001 From: themanoncode Date: Mon, 9 Jan 2023 11:42:49 +0100 Subject: [PATCH] deposit and deduct from the wallet at the database level --- src/Traits/HasWallet.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index afd6504..3c13ada 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -12,13 +12,9 @@ public function deposit(int|float $amount): float|int { $this->throwExceptionIfAmountIsInvalid($amount); - $balance = $this->wallet_balance ?? 0; - - $balance += $amount; + $this->increment('wallet_balance', $amount); - $this->forceFill(['wallet_balance' => $balance])->save(); - - return $balance; + return $this->wallet_balance; } public function withdraw(int|float $amount): float|int @@ -27,11 +23,9 @@ public function withdraw(int|float $amount): float|int $this->throwExceptionIfFundIsInsufficient($amount); - $balance = $this->wallet_balance - $amount; - - $this->forceFill(['wallet_balance' => $balance])->save(); + $this->decrement('wallet_balance', $amount); - return $balance; + return $this->wallet_balance; } public function canWithdraw(int|float $amount): bool