roundly-consulting / money-for-laravel
Immutable arbitrary-precision Money and Currency value objects for Laravel: ISO 4217 + custom currencies, exact decimal parsing, rounding-safe arithmetic and allocation, locale formatting, Eloquent casts and schema macros, validation, discounts, tax, and pluggable exchange rates (array, database, EC
Package info
github.com/roundly-consulting/money-for-laravel
pkg:composer/roundly-consulting/money-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- ext-bcmath: *
- illuminate/bus: ^12.0|^13.0
- illuminate/cache: ^12.0|^13.0
- illuminate/console: ^12.0|^13.0
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/http: ^12.0|^13.0
- illuminate/queue: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- illuminate/validation: ^12.0|^13.0
- illuminate/view: ^12.0|^13.0
- roundly-consulting/enums-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.5
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
- ext-intl: Locale-aware money formatting and localized input parsing (falls back to a deterministic formatter without it).
- ext-simplexml: Required by the ECB exchange-rate provider (enabled by default in standard PHP builds).
Provides
None
Conflicts
None
Replaces
None
README
Money for Laravel
Immutable, arbitrary-precision Money and Currency value objects for Laravel: exact decimal
parsing, rounding-safe arithmetic and allocation, locale formatting, Eloquent casts, validation,
discounts, tax and pluggable exchange rates. Amounts are minor units held as integer strings with
bcmath arithmetic, so there is no float anywhere and no int64 ceiling.
Installation
Requires PHP 8.4 (ext-bcmath; ext-intl for locale-aware formatting) and Laravel 12 or 13.
composer require roundly-consulting/money-for-laravel
Usage
Calculate with exact amounts. Every lossy step rounds once, and currencies never mix silently:
use RoundlyConsulting\Money\Money; $price = Money::ofMajor('19.99', 'EUR'); // minor "1999", never a float $total = $price->add(Money::ofMinor(250, 'EUR'))->multiply(3); // 67.47 EUR, exact [$a, $b, $c] = $total->allocate(1, 1, 1); // parts always sum back to $total $total->add(Money::ofMajor('1', 'USD')); // throws CurrencyMismatch $total->toDecimal(); // "67.47" $total->format('sk'); // "67,47 €"
Store it on a model: $table->money('price') creates the amount and currency columns:
use RoundlyConsulting\Money\Casts\AsMoney; Schema::create('products', function (Blueprint $table) { $table->id(); $table->money('price'); // decimal(38,0) price + price_currency $table->timestamps(); }); final class Product extends Model { protected function casts(): array { return ['price' => AsMoney::class]; } } $product = Product::create(['price' => Money::ofMajor('19.99', 'EUR')]);
Convert with the Exchange facade (ECB rates by default, no setup):
use RoundlyConsulting\Money\Facades\Exchange; $product->price->convertTo('USD'); // Money in USD at today's rate Exchange::convertWithRate($product->price, 'CZK'); // Conversion: original, converted, rate
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/money-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). See LICENSE.md.