Many of our clients work in more than one currency. An expense is in US dollars, an invoice is in euros, and the report must show the total in Swedish kronor. Each project needed the same parts: a source for the exchange rates, a cache, and a base-currency copy of each amount.
We put these parts into a package: Laravel Currency Converter.
What the package does
- It converts an amount from one currency to a different currency.
- It gets the exchange rates from a provider. You can change the provider in the config.
- It keeps each rate in the cache for one day, so it does not call the provider for each conversion.
- It gives a trait that keeps a base-currency copy of the amounts on a model.
- It gives a fake for your tests.
Install the package
composer require bernskiold/laravel-currency-converter
You do not need an API key to start. The default provider is Frankfurter. It is free, and it uses the reference rates of the European Central Bank.
Convert an amount
Use the facade:
use Bernskiold\LaravelCurrencyConverter\Facades\CurrencyConverter;
CurrencyConverter::convert(100, 'USD', 'SEK'); // 1050.0
CurrencyConverter::rate('USD', 'SEK'); // 10.5
Set your base currency in the config. Then use toBase() and fromBase():
CurrencyConverter::toBase(100, 'USD'); // From USD to the base currency
CurrencyConverter::fromBase(100, 'USD'); // From the base currency to USD
To show an amount to a person, use format():
CurrencyConverter::format(1234.5, 'USD'); // "1,234.50 USD"
When a conversion fails, the package throws one exception: CurrencyConversionException. You decide what your application does then.
Keep a base-currency copy on a model
You often keep an amount in the currency of the record. For reports, you also need the amount in one currency. The ConvertsCurrencies trait keeps this copy for you.
Tell the trait which column to convert, and where to keep the result:
use Bernskiold\LaravelCurrencyConverter\Concerns\ConvertsCurrencies;
class Expense extends Model
{
use ConvertsCurrencies;
protected static array $currencyConversions = [
'amount' => 'amount_sek',
];
}
The trait reads the currency from the currency column. It converts the amount when you create the model. On an update, it converts the amount again only when the amount or the currency changes. If the record is already in the base currency, the trait copies the amount and does not call the provider.
The model also gets two helpers for your views:
$expense->amountWithCurrency('amount'); // "1,234.56 USD"
$expense->amountInBaseCurrency('amount'); // "12,962.88 SEK"
Select a provider
The package has drivers for Frankfurter, ExchangeRate-API, exchangerate.host, Open Exchange Rates and Fixer. Some of these need an API key. Set the default driver in the config, or select a driver for one call:
CurrencyConverter::convert(100, 'USD', 'SEK', driver: 'exchangerate_host');
Sometimes you must use fixed rates that you can audit. For this, use the database driver. It reads the rates from an exchange_rates table. The ExchangeRate model sets the rates:
use Bernskiold\LaravelCurrencyConverter\Models\ExchangeRate;
ExchangeRate::setRate('USD', 'SEK', 10.42);
To use a different source, write a class that implements ExchangeRateProvider. Then register it with CurrencyConverter::extend().
Test your code
Use the fake in your tests. It does not call a provider, and it records each conversion.
CurrencyConverter::fake(['USD' => ['SEK' => 10.0]]);
// Run your code
CurrencyConverter::assertConverted('USD', 'SEK');
The fake also works with the ConvertsCurrencies trait.
A limit to know
If a conversion in the trait fails, the trait does not stop the save. It writes the error to the log, and the base-currency column does not get a new value. Later, you can calculate the values again:
$expense->recalculateCurrencyConversions();
Get the package
Laravel Currency Converter is open source, with the MIT license. The code and the full documentation are on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.