Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Wallet Laravel Package

zotel/laravel-wallet

View on GitHub
Deep Wiki
Context7

Transfer between wallets

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 HasWallets trait's and Wallet interface.

use Zotel\Wallet\Traits\HasWallets;
use Zotel\Wallet\Interfaces\Wallet;

class User extends Model implements Wallet
{
    use HasWallets;
}

Make a Transfer

Find user:

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

Create new wallets for users.

$firstWallet = $first->createWallet(['name' => 'First User Wallet']);
$lastWallet = $last->createWallet(['name' => 'Second User Wallet']);

$firstWallet->deposit(100);
$firstWallet->balance; // 100
$lastWallet->balance; // 0

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

$firstWallet->transfer($lastWallet, 5); 
$firstWallet->balance; // 95
$lastWallet->balance; // 5

It's simple!

Force Transfer

Check the user's balance.

$firstWallet->balance; // 100
$lastWallet->balance; // 0

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

$firstWallet->forceTransfer($lastWallet, 500); 
$firstWallet->balance; // -400
$lastWallet->balance; // 500

It worked!

Change the meta and confirmation

Check the user's balance.

$firstWallet->balanceInt; // 1_000
$secondWallet->balanceInt; // 0

We will execute the transfer, but without confirmation of the withdrawal of funds.

use Zotel\Wallet\External\Dto\Extra;
use Zotel\Wallet\External\Dto\Option;

/** [@var](https://github.com/var) $firstWallet \Zotel\Wallet\Interfaces\Wallet */
$transfer = $firstWallet->transfer($secondWallet, 500, new Extra(
    deposit: ['message' => 'Hello, secondWallet!'],
    withdraw: new Option(meta: ['something' => 'anything'], confirmed: false)
));

$transfer->withdraw->meta; // ['something' => 'anything']
$transfer->withdraw->confirmed; // false

$transfer->deposit->meta; // ['message' => 'Hello, secondWallet!']
$transfer->deposit->confirmed; // true

$firstWallet->balanceInt; // 1_000
$secondWallet->balanceInt; // 500

It's simple!

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle