Laravel Packages

Handle Money Transactions in Eloquent with Laravel Wallet

Published Updated
Handle Money Transactions in Eloquent with Laravel Wallet image

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 recipient
transfer(100, 'USD')->from($sender)->to($recipient)->commit();

// Deposit money into a recipient's account
deposit(100, 'USD')->to($recipient)->overcharge()->commit();

// Charge a user
charge(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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article
PIE 1.5 Changes PHP Extension Installs in CI image

PIE 1.5 Changes PHP Extension Installs in CI

Read article
Delete Expired Sessions in Laravel With a Scheduled Command image

Delete Expired Sessions in Laravel With a Scheduled Command

Read article