A Simple Wallet Implementation for Laravel

Packages

May 17th, 2022

laravel-money.jpg

Laravel Wallet by Stephen Jude is a simple wallet implementation for Laravel models:

1class User extends Authenticatable implements Wallet
2{
3 use HasWallet;
4}

Using the HasWallet trait, you can add deposits, make withdrawals, and check the balance of the wallet for a given model:

1// Making deposits
2 
3// returns the wallet balance: 200.22
4$user->deposit(200.22);
5 
6// returns the wallet balance: 400.22
7$user->deposit(200);

Given a wallet balance from the above example, you can make withdrawals:

1// returns the wallet balance: 200.22
2$user->withdraw(200);
3 
4// returns the wallet balance: 200
5$user->withdraw(0.22);

If you attempt to withdraw more than the amount in the wallet, the package will throw an exception:

1use Stephenjude\Wallet\Exceptions\ InsufficientFundException;
2 
3try {
4 $user->withdraw(5_000);
5} catch (InsufficientFundException $e) {
6 // Code to handle insufficient funds...
7}

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners