Laravel Wallet by Stephen Jude is a simple wallet implementation for Laravel models:
1class User extends Authenticatable implements Wallet2{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 deposits2 3// returns the wallet balance: 200.224$user->deposit(200.22);5 6// returns the wallet balance: 400.227$user->deposit(200);
Given a wallet balance from the above example, you can make withdrawals:
1// returns the wallet balance: 200.222$user->withdraw(200);3 4// returns the wallet balance: 2005$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:
Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.