arsamme / laravel-wallet
Virtual wallet implementation to use in Laravel projects.
Fund package maintenance!
Open Collective
www.tinkoff.ru/cf/1OkIPwTvTLf
Requires
- php: ^8.2
- ext-json: *
- ext-pdo: *
- brick/math: ~0.10
- illuminate/contracts: ^11.0|^12.0
- illuminate/database: ^11.0|^12.0
- ramsey/uuid: ^4.0
Requires (Dev)
- laravel/framework: ^11.0|^12.0
- laravel/pint: ^1.22
- nunomaduro/collision: ^8.0
- phpunit/phpunit: ^11.0
This package is auto-updated.
Last update: 2025-04-17 04:25:11 UTC
README
New Wallet
You can create an unlimited number of wallets, but the slug
for each wallet should be unique.
User Model
Add the HasWallets
trait's and Wallet
interface to model.
use Bavix\Wallet\Traits\HasWallets; use Bavix\Wallet\Interfaces\Wallet; class User extends Model implements Wallet { use HasWallets; }
Create a wallet
Find user:
$user = User::first();
Create a new wallet.
$user->hasWallet('my-wallet'); // bool(false) $wallet = $user->createWallet([ 'name' => 'New Wallet', 'slug' => 'my-wallet', ]); $user->hasWallet('my-wallet'); // bool(true) $wallet->deposit(100); $wallet->balance; // 100 $wallet->balanceFloatNum; // 1.00
How to get the right wallet?
$myWallet = $user->getWallet('my-wallet'); $myWallet->balance; // 100 $myWallet->balanceFloatNum; // 1.00
Default Wallet + MultiWallet
Is it possible to use the default wallet and multi-wallets at the same time? Yes.
use Bavix\Wallet\Traits\HasWallet; use Bavix\Wallet\Traits\HasWallets; use Bavix\Wallet\Interfaces\Wallet; class User extends Model implements Wallet { use HasWallet, HasWallets; }
How to get the default wallet?
$wallet = $user->wallet; $wallet->balance; // 10 $wallet->balanceFloatNum; // 0.10
It's simple!