Skip to content

To refresh the balance ​

There are situations when you create a lot of unconfirmed operations, and then abruptly confirm everything. In this case, the user's balance will not change. You must be forced to refresh the balance.

User Model ​

It is necessary to expand the model that will have the wallet. This is done in two stages:

  • Add Wallet interface;
  • Add the HasWallet trait;

Let's get started.

php
use Bavix\Wallet\Traits\HasWallet;
use Bavix\Wallet\Interfaces\Wallet;

class User extends Model implements Wallet
{
    use HasWallet;
}

The model is prepared to work with a wallet.

Get the current balance for your wallet ​

Let's say the user's balance

php
$user->id; // 5
$user->balance; // 27

And he has unconfirmed transactions. Confirm all transactions.

sql
update transactions 
set confirmed=1 
where confirmed=0 and 
      payable_type='App\Models\User' and 
      payable_id=5;
-- 212 rows affected in 54 ms

Refresh the balance.

php
$user->balance; // 27
$user->wallet->refreshBalance();
$user->balance; // 42

It's simple!