elegantly / laravel-money
Use Brick/Money in your Laravel app
Fund package maintenance!
ElegantEngineeringTech
Installs: 23 900
Dependents: 3
Suggesters: 0
Security: 0
Stars: 14
Watchers: 1
Forks: 2
Open Issues: 0
pkg:composer/elegantly/laravel-money
Requires
- php: ^8.1
- brick/money: ^0.10.0
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^8.22.0||^9.0.0||^10.0.0
- pestphp/pest: ^3.0||^4.0
- pestphp/pest-plugin-arch: ^3.0||^4.0
- pestphp/pest-plugin-laravel: ^3.0||^4.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
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. - 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.
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.