Handle Money Transactions in Eloquent with Laravel Wallet
Last updated on by Paul Redmond
Laravel Wallet is designed for reliable and flexible transactions and handles the complex part of storing transactions in the database for precise calculations. It provides an intuitive API that is easy to use and straightforward to understand.
Once you have followed the setup instructions, you can set up the user model with the HasBalance
trait:
namespace App\Models; use Illuminate\Database\Eloquent\Model;use O21\LaravelWallet\Contracts\Payable;use O21\LaravelWallet\Models\Concerns\HasBalance; class User extends Model implements Payable{ use HasBalance;}
Here are some possible commands you can use to work with a balance on the model. In the following examples, the $sender
and $recipient
implement the Payable
interface via the HasBalance
trait:
// Transfer money between sender and recipienttransfer(100, 'USD')->from($sender)->to($recipient)->commit(); // Deposit money into a recipient's accountdeposit(100, 'USD')->to($recipient)->overcharge()->commit(); // Charge a usercharge(100, 'USD')->from($sender)->commit();
This package has resulted from the author using it privately on projects, honing it, and now releasing it as open-source. It includes safeguards for balance accuracy and a sophisticated rollback mechanism designed to revert modifications.
Learn More
To get started with this package, check out the GitHub page.