Wallet logo Wallet

Transfer

Transfer in our system are two well-known Deposit and Withdraw operations that are performed in one transaction.

The transfer takes place between wallets.


User Model

Prepare the model, add the HasWallet trait and Wallet interface.

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

class User extends Model implements Wallet
{
    use HasWallet;
}

Make a Transfer

Find user:

$first = User::first(); 
$last = User::orderBy('id', 'desc')->first(); // last user
$first->getKey() !== $last->getKey(); // true

As the user uses HasWallet, he will have balance property. Check the user's balance.

$first->balance; // int(100)
$last->balance; // int(0)

The transfer will be from the first user to the second.

$first->transfer($second, 5); 
$first->balance; // int(95)
$second->balance; // int(5)

It worked!

Force Transfer

Check the user's balance.

$first->balance; // int(100)
$last->balance; // int(0)

The transfer will be from the first user to the second.

$first->forceTransfer($second, 500); 
$first->balance; // int(-400)
$second->balance; // int(500)

It worked!