Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Money Laravel Package

brick/money

Immutable money & currency library for PHP with exact arithmetic, explicit rounding control, and support for any-sized amounts. Built on brick/math to avoid floating-point errors; works with ISO currencies and integrates well with GMP/BCMath for speed.

View on GitHub
Deep Wiki
Context7

brick/money is an immutable money & currency library for PHP that focuses on exact arithmetic and explicit rounding control, helping you avoid the silent errors of floating-point calculations. Built on brick/math, it supports precise operations on monetary values of any size and is suitable for production use.

Key features

  • Immutable Money/Currency value objects for safer domain modeling
  • Exact calculations (no floating-point drift), even with very large amounts
  • Explicit, configurable rounding for predictable financial results
  • Broad currency support and formatting-friendly representations
  • Optional performance boosts via GMP or BCMath extensions
Frequently asked questions about Money
How do I integrate brick/money into a Laravel project for precise financial calculations?
Install via Composer (`composer require brick/money`), then replace primitive floats/ints with `Money` objects in your domain models. Use accessors/mutators for Eloquent serialization (e.g., `protected $price` with `getPriceAttribute()`). For API responses, implement `JsonSerializable` or use custom encoders like `Money::getAmount()` + `Money::getCurrency()`.
Will brick/money work with Laravel’s Eloquent ORM for database storage?
Yes. Store monetary values as minor units (e.g., `999` for $9.99) in a `DECIMAL` or `BIGINT` column, paired with a `CHAR(3)` currency code (ISO 4217). For context serialization (e.g., rounding rules), use a JSON column or separate fields. Avoid storing full `Money` objects directly to prevent precision loss.
Does brick/money support Laravel’s validation rules for currency and rounding?
Yes. Use Laravel’s built-in validation rules (e.g., `required|string|size:3` for currency codes) alongside custom validation logic for rounding modes or context parameters. For example, validate `CashContext` step values or ensure currency codes match ISO 4217 standards before processing.
What Laravel versions and PHP requirements does brick/money support?
The latest stable version requires **PHP 8.2+** and works seamlessly with Laravel 9/10. For older PHP (8.1, 8.0, or 7.x), downgrade to versions `0.10`, `0.8`, or `0.5`, but note these are EOL and may lack long-term support. Always pin to a minor version (e.g., `0.13.*`) to avoid breaking changes.
How do I handle currency conversions or exchange rates in Laravel with brick/money?
Use `brick/math` for conversions (e.g., `Money::divide()` with exchange rates) or integrate a third-party service like `moneyphp/money`. Cache exchange rates in Redis for performance, and store them as separate columns in your database (e.g., `exchange_rate DECIMAL(19,6)`). Avoid recalculating rates in real-time for critical paths.
Are there performance concerns with brick/money in production, especially for large-scale operations?
For optimal performance, enable **GMP** or **BCMath** extensions to handle large calculations efficiently. Avoid `RationalMoney` for batch operations (e.g., splitting 1M orders) due to memory overhead. Benchmark critical paths against native PHP arithmetic if GMP/BCMath isn’t available, but expect slower execution for non-trivial operations.
Can I use brick/money with Laravel queues or background jobs for order processing?
Yes. `Money` objects are immutable and thread-safe, making them ideal for queues/jobs (e.g., order fulfillment). Serialize them via `JsonSerializable` or custom encoders when dispatching jobs. For example, store `Money` as an array in the job payload: `['amount' => 999, 'currency' => 'USD']` and reconstruct it on the worker side.
How do I test brick/money in Laravel unit tests, including edge cases like rounding or currency mismatches?
Mock `Money` objects using a library like `mocks-for-php` or create test doubles for specific currencies/rounding modes. Test edge cases like `Money::add()` with mismatched currencies, `CashContext` rounding rules, and large numbers (e.g., `Money::multiply()` with 1000x scaling). Use PHPUnit assertions to verify exact arithmetic (e.g., `assertEquals(999, $money->getAmount())`).
What are the alternatives to brick/money for Laravel financial applications?
Alternatives include `moneyphp/money` (simpler but less precise) and `league/money` (feature-rich but heavier). `brick/money` stands out for its **immutability**, **exact arithmetic**, and **Laravel-friendly design**, making it ideal for domains requiring strict financial compliance (e.g., invoicing, accounting). If you need cryptocurrency support, consider `satooshi/php-bitcoin-tool` alongside `brick/money`.
How do I handle breaking changes when upgrading brick/money in a Laravel project?
Pin to a minor version (e.g., `0.13.*`) to avoid unexpected breaks. Check the [release history](https://github.com/brick/money/releases) for changes in new `0.x.0` cycles. For major upgrades, test thoroughly in a staging environment, especially for custom contexts or rounding logic. Use Laravel’s service container to bind `Money` interfaces if you need to swap implementations later.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport