elegantly/laravel-money
Laravel integration for brick/money: Eloquent casting to Brick\Money\Money, safe parsing from strings/ints/floats, and a ValidMoney validation rule with min/max bounds and nullability. Store amounts cleanly (with currency column or fixed currency).
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.
Brick\Money\Money.Money instances safely.Install via Composer:
composer require elegantly/laravel-money
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',
];
For maximum precision, store money using:
bigInteger column for the amount (in the smallest currency unit)string column for the ISO currency codeThis 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();
});
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'),
];
}
}
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'),
];
}
}
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.
ValidMoney in Livewirenamespace 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;
}
ValidMoney in Form Requestsnamespace 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
),
],
];
}
}
Run the test suite:
composer test
See the CHANGELOG for a full history of updates.
Contributions are welcome! Review the CONTRIBUTING file for details.
If you discover a security vulnerability, please refer to the security policy.
This package is open-source software released under the MIT License. See LICENSE.md for details.
How can I help you explore Laravel packages today?