Handle Money Transactions in Eloquent with Laravel Wallet

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 Laravel Wallet documentation—this package is open source and available on GitHub. The documentation also includes a Withdrawal example to demonstrate best practices with this package.


The post Handle Money Transactions in Eloquent with Laravel Wallet appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.

Read more

© 2024 Extly, CB - All rights reserved.