- How do I install moneyphp/money in a Laravel project?
- Run `composer require moneyphp/money` in your project root. No Laravel-specific configuration is needed—it works out-of-the-box with PHP 8.1+ (Laravel’s LTS version). Ensure BCMath is enabled in your `php.ini` (default in Laravel’s PHP builds).
- Can I use this package with Laravel Eloquent models?
- Yes. Store Money objects as JSON in database columns (e.g., `json` type) or use accessors/mutators to convert between Money objects and database values. For relational queries, consider Doctrine extensions or custom Eloquent logic.
- Does moneyphp/money support dynamic currency exchange rates?
- The package includes static exchange logic (e.g., Swap). For dynamic rates, integrate with APIs (e.g., ExchangeRate-API) and cache responses in Laravel’s Redis or database. Use the `Money::create()` method with fetched rates.
- How do I migrate from float-based monetary fields to Money objects in Laravel?
- Use Laravel migrations to add new `json` columns for Money objects, then update models with accessors/mutators. For legacy data, write a one-time script to convert floats to Money objects (e.g., `Money::EUR((int)($value * 100))`).
- Is moneyphp/money compatible with Laravel’s API resources?
- Yes. Implement `Arrayable` or override `toArray()` in your API resources to serialize Money objects to arrays/JSON. Example: `return ['amount' => $money->getAmount()];`. Works seamlessly with Sanctum/Passport responses.
- What Laravel versions does moneyphp/money support?
- The package requires PHP 8.1+ (Laravel 9+ LTS) and works with all recent Laravel versions. For older PHP (e.g., 7.4 for Laravel 8), use `moneyphp/money:^3.0`. Always check the [PHP support policy](https://www.php.net/supported-versions.php).
- How do I handle money operations in Laravel queues/jobs?
- Money objects are JSON-serializable via `jsonSerialize()`, so they work with Laravel Queues. For jobs, implement `Serializable` or use `Money::jsonSerialize()` in your job’s `handle()` method. Handle `OverflowException` with Laravel’s job retries.
- Are there alternatives to moneyphp/money for Laravel?
- Other options include `league/money` (similar DDD approach) or `brianium/paradox` (for complex financial math). However, `moneyphp/money` is the most mature, Laravel-compatible choice with built-in ISO currencies, formatting, and BCMath/GMP support.
- How do I format Money objects for user display in Laravel?
- Use the built-in `MoneyFormatter` (e.g., `MoneyFormatter::setCurrency($money->getCurrency())->format($money)`) or IntlFormatter for locale-aware output. For API responses, return raw amounts (e.g., `getAmount()`) and let frontend handle formatting.
- Can I test Money objects with Laravel’s Pest framework?
- Yes. The package includes PHPUnit examples, but adapting to Pest is straightforward. Example: `expect(Money::EUR(100)->add(Money::EUR(50)))->toEqual(Money::EUR(150))`. Use Pest’s `assertEquals()` or `expect()` syntax for assertions.