A Simple Wallet Implementation for Laravel
Published on by Paul Redmond
Laravel Wallet by Stephen Jude is a simple wallet implementation for Laravel models:
class User extends Authenticatable implements Wallet{ use HasWallet;}
Using the HasWallet
trait, you can add deposits, make withdrawals, and check the balance of the wallet for a given model:
// Making deposits // returns the wallet balance: 200.22$user->deposit(200.22); // returns the wallet balance: 400.22$user->deposit(200);
Given a wallet balance from the above example, you can make withdrawals:
// returns the wallet balance: 200.22$user->withdraw(200); // returns the wallet balance: 200$user->withdraw(0.22);
If you attempt to withdraw more than the amount in the wallet, the package will throw an exception:
use Stephenjude\Wallet\Exceptions\ InsufficientFundException; try { $user->withdraw(5_000);} catch (InsufficientFundException $e) { // Code to handle insufficient funds...}
You can learn more about this package, get full installation instructions, and view the source code on GitHub.