finller/laravel-money
| Install | |
|---|---|
composer require finller/laravel-money |
|
| Latest Version: | v4.2.2 |
| PHP: | ^8.3 |
| Laravel: | 13 |
| Older releases: | 9 – 12 |
| License: | MIT |
| Last Updated: | Sep 29, 2026 |
| Links: | GitHub · Packagist |
Elegant Integration of Brick/Money for Laravel
Table of Contents
Introduction
This package provides seamless, expressive integration of Brick/Money with Laravel. It enables safe, precise handling of monetary values in your application—using value objects, smart casting, robust parsing, and powerful validation tools.
Features
- MoneyCast – Automatically cast Eloquent attributes to
Brick\Money\Money. - MoneyParser – Convert strings, integers, or floats into
Moneyinstances safely. - sumMoney – Sum money values from an iterable using a key or callback.
- ValidMoney Rule – Validate monetary input with min/max boundaries, type safety, and nullability.
Installation
Install via Composer:
composer require elegantly/laravel-money
Configuration
Publish the configuration file if you need to customize defaults:
php artisan vendor:publish --tag="money-config"
Default config (config/money.php):
return [
'default_currency' => 'USD',
];
Storing Money in the Database
For maximum precision, store money using:
- a
bigIntegercolumn for the amount (in the smallest currency unit) - a
stringcolumn for the ISO currency code
This avoids floating-point precision issues and ensures accurate calculations.
Example migration:
Schema::create('invoices', function (Blueprint $table) {
$table->id();
$table->bigInteger('amount'); // e.g., 1000 = $10.00
$table->string('currency', 3); // ISO 4217 code
$table->timestamps();
});
Usage
Casting with a Currency Column (Recommended)
If your model stores both amount and currency, reference the currency column in the cast:
use Elegantly\Money\MoneyCast;
use Brick\Money\Money;
/**
* @property Money $amount
* @property string $currency
*/
class Invoice extends Model
{
protected function casts(): array
{
return [
'amount' => MoneyCast::of('currency'),
];
}
}
Casting with a Fixed Currency
If the currency is known and constant, define it directly:
use Elegantly\Money\MoneyCast;
use Brick\Money\Money;
/**
* @property Money $amount
* @property Money $cost
*/
class Invoice extends Model
{
protected function casts(): array
{
return [
'cost' => MoneyCast::of('EUR'),
'price' => MoneyCast::of('USD'),
];
}
}
Parsing Values
MoneyParser converts common numeric and string formats into Money instances:
use Elegantly\Money\MoneyParser;
MoneyParser::parse(null, 'EUR'); // null
MoneyParser::parse(110, 'EUR'); // 110.00 €
MoneyParser::parse(100.10, 'EUR'); // 100.10 €
MoneyParser::parse('', 'EUR'); // null
MoneyParser::parse('1', 'EUR'); // 1.00 €
MoneyParser::parse('100.10', 'EUR'); // 100.10 €
The parser handles nullability, empty strings, integers, floats, and decimal string formats gracefully.
Summing Money Values
Use sumMoney() to add Money values from an iterable. Pass a key (resolved with Laravel's data_get) or a callback that returns a Money value or null:
use Brick\Money\Money;
use function Elegantly\Money\sumMoney;
$items = [
['price' => Money::of('10.25', 'EUR')],
['price' => null],
['price' => Money::of('4.75', 'EUR')],
];
sumMoney($items, 'price'); // Money::of('15.00', 'EUR')
sumMoney($items, fn (array $item) => $item['price']); // Same result
Null values are skipped. If the iterable is empty or contains no Money values, the result is null. An optional third argument accepts a Brick\Math\RoundingMode; otherwise, the configured rounding mode is used. Values must have compatible currencies.
Validation Rule
Using ValidMoney in Livewire
namespace App\Livewire;
use Elegantly\Money\Rules\ValidMoney;
use Livewire\Component;
class CustomComponent extends Component
{
#[Validate([
new ValidMoney(nullable: false, min: 0, max: 100),
])]
public ?int $price = null;
}
Using ValidMoney in Form Requests
namespace App\Http\Requests;
use Elegantly\Money\Rules\ValidMoney;
use Illuminate\Foundation\Http\FormRequest;
class CustomFormRequest extends FormRequest
{
public function rules()
{
return [
'price' => [
new ValidMoney(
nullable: false,
min: 0,
max: 100
),
],
];
}
}
Testing
Run the test suite:
composer test
Changelog
See the CHANGELOG for a full history of updates.
Contributing
Contributions are welcome! Review the CONTRIBUTING file for details.
Security
If you discover a security vulnerability, please refer to the security policy.
Credits
License
This package is open-source software released under the MIT License. See LICENSE.md for details.
Related Packages
This provides Laravel with currency functions such as currency formatting and co...
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v4.2.2 | ^8.3 | ^13.0 | MIT | |
| v4.2.1 | ^8.3 | ^13.0 | MIT | |
| v4.2.0 | ^8.3 | ^13.0 | MIT | |
| v4.1.0 | ^8.3 | ^13.0 | MIT | |
| v4.0.3 | ^8.3 | ^13.0 | MIT | |
| v4.0.2 | ^8.3 | ^13.0 | MIT | |
| v4.0.1 | ^8.3 | ^13.0 | MIT | |
| v4.0.0 | ^8.1 | ^12.0| | MIT | |
| v3.0.0 | ^8.1 | ^12.0| | MIT | |
| v2.4.1 | ^8.1 | ^12.0| | MIT | |
| v2.4.0 | ^8.1 | ^12.0| | MIT | |
| v2.3.4 | ^8.1 | ^12.0 | MIT | |
| v2.3.3 | ^8.1 | ^10.0| | MIT | |
| v2.3.2 | ^8.1 | ^10.0| | MIT | |
| v2.3.1 | ^8.1 | ^10.0| | MIT | |
| v2.3.0 | ^8.1 | ^10.0| | MIT | |
| v2.2.3 | ^8.1 | ^10.0| | MIT | |
| v2.2.2 | ^8.1 | ^10.0| | MIT | |
| v2.2.1 | ^8.1 | ^10.0| | MIT | |
| v2.2.0 | ^8.1 | ^10.0| | MIT | |
| v2.1.1 | ^8.1 | ^10.0| | MIT | |
| v2.1.0 | ^8.1 | ^10.0| | MIT | |
| v2.0.1 | ^8.1 | ^10.0| | MIT | |
| v2.0.0 | ^8.1 | ^10.0| | MIT | |
| v1.0.4 | ^8.1 | ^10.0| | MIT | |
| v1.0.3 | ^8.1 | ^10.0| | MIT | |
| v1.0.2 | ^8.1 | ^10.0 | MIT | |
| v1.0.1 | ^8.1 | ^10.0 | MIT | |
| v1.0.0 | ^8.1 | ^10.0 | MIT | |
| v0.1.3 | ^8.1 | ^9.0 | MIT |
Showing the latest 30 of 33. See every release on Packagist