- How do I install sylius/currency in a Laravel project?
- Run `composer require sylius/currency` to install the package. Then register the `CurrencyServiceProvider` in your `config/app.php` under the `providers` array. No additional Laravel version requirements beyond PHP 8.1+ are needed.
- Does this package support real-time currency conversion for eCommerce?
- Yes, it supports real-time conversions by integrating with external rate providers (e.g., ECB APIs) or internal databases. Use Laravel’s caching (e.g., Redis) to optimize performance for high-frequency conversions like checkout flows.
- Can I use sylius/currency with Laravel’s Eloquent models for product pricing?
- Absolutely. Extend your Eloquent models (e.g., `Product`) with currency-aware accessors like `getPriceIn(Currency $currency)` or traits to dynamically convert and store prices in multiple currencies.
- What Laravel versions does sylius/currency officially support?
- The package is compatible with Laravel 9.x and 10.x, as it requires PHP 8.1+. Check the [Sylius documentation](https://docs.sylius.com) for version-specific notes, but it avoids hard dependencies on Sylius core for standalone use.
- How do I handle rounding errors in currency conversions (e.g., 0.10 + 0.20 ≠ 0.30)?
- Use arbitrary-precision libraries like `bcmath` or pair with `moneyphp/money` for exact arithmetic. The package itself doesn’t enforce rounding but provides hooks to integrate custom strategies (e.g., `round-half-up`).
- Is there built-in support for cryptocurrencies like Bitcoin or Ethereum?
- No, the package focuses on ISO 4217 currencies (e.g., USD, EUR). For crypto, extend the `Currency` class or use a custom provider to validate and format non-standard symbols (e.g., BTC). Pair with a dedicated crypto library for rate fetching.
- How can I test currency conversions in Laravel’s testing environment?
- Use Laravel’s `Mockery` or `PestPHP` to mock external rate providers. The package includes test utilities for validating ISO codes and conversion logic. For edge cases (e.g., invalid currencies), leverage Laravel’s `assertThrows` or custom test doubles.
- Does sylius/currency work with Laravel’s API Resources for JSON responses?
- Yes. Format API responses with currency metadata (e.g., `amount`, `currency_code`, `formatted_value`) using Laravel’s `ApiResource`. Example: `return new CurrencyResource($order->amountIn(EUR));` for localized pricing.
- What are the alternatives to sylius/currency for Laravel projects?
- Consider `moneyphp/money` for advanced arithmetic or `spatie/currency` for simpler ISO 4217 validation. Sylius’s package stands out for its event-driven architecture (e.g., `CurrencyConverted`) and Laravel synergy, ideal for complex eCommerce or financial apps.
- How do I cache exchange rates to avoid hitting external APIs repeatedly?
- Use Laravel’s cache drivers (e.g., Redis, file) to store rates with TTLs. Example: `Cache::remember('eur_usd_rate', 3600, fn() => $provider->fetchRate('EUR', 'USD'))`. For critical systems, implement a fallback to stale data or manual overrides.