- How do I install elegantly/laravel-money in a Laravel app?
- Install via Composer with composer require elegantly/laravel-money. Laravel auto-discovers the service provider. Optionally publish the config file with php artisan vendor:publish --tag="money-config" to customize defaults.
- How do I cast an Eloquent attribute to a Brick Money object?
- Use the MoneyCast cast on your model attribute. Define it in the $casts array, either pointing to a currency column or passing a fixed currency. The attribute will then return a Brick\Money\Money instance.
- What database columns should I use to store money with laravel-money?
- The recommended schema is a bigInteger column for the amount in the smallest currency unit and a string column for the ISO currency code. This avoids floating-point errors and supports precision. Existing decimal or float columns can be parsed but may sacrifice precision.
- How does laravel-money handle multi-currency models?
- You can cast with a currency column so each record stores its own currency, enabling dynamic multi-currency support. Alternatively, use a fixed currency per model or field when the currency is constant. This supports global applications without architectural changes.
- How do I validate monetary input in Laravel with this package?
- Use the provided ValidMoney validation rule in Form Requests or inline validators. It supports min/max boundaries, type safety, and nullability. This helps sanitize API and user-facing monetary input.
- How can I parse strings or numbers into Money objects?
- The MoneyParser converts strings, integers, and floats into Brick\Money\Money instances safely. It handles common monetary formats and eases integration with external systems. Use it when you need to normalize input before storage or calculation.
- Which Laravel versions does elegantly/laravel-money support?
- The package officially supports Laravel 10 through 12. It integrates with Eloquent, Form Requests, Livewire, and validation pipelines. Check the repository for any future version updates.
- Can I use laravel-money with existing decimal or float database columns?
- Backward-compatible parsing exists, but storing money as decimal or float can sacrifice precision. For optimal precision, migrate to bigInteger amount plus string currency columns. Plan a careful rounding strategy if migrating existing data.
- How are Money objects serialized to JSON in APIs?
- Money objects serialize to floats by default, such as 100.00. If your API contract expects strings like "USD 100.00", customize serialization in your resources or casts. Consider standardizing output format across endpoints.
- Does laravel-money support currency conversion or exchange rates?
- No, the package focuses on storage, casting, parsing, and validation of monetary values. It does not provide real-time currency conversion or exchange rates. Integrate a separate exchange rate service if conversion is required.