A currency exchange rate library for Laravel
juanparati/laravel-exchanger is a Laravel package for a currency exchange rate library for laravel.
It currently has 0 GitHub stars and 3.836 downloads on Packagist (latest version 13.1).
Install it with composer require juanparati/laravel-exchanger.
Discover more Laravel packages by juanparati
or browse all Laravel packages to compare alternatives.
Last updated
A Laravel currency converter library that uses florianv/exchanger.
composer require juanparati/laravel-exchanger
Publish the configuration file:
artisan vendor:publish --provider="Juanparati\LaravelExchanger\Providers\ExchangerServiceProvider"
The configuration contains a list of services, check the florianv/exchanger documentation in order to know the description and characteristics of each service.
It's important to provide a valid cache time (in seconds) in order to avoid duplicate requests.
$rate = Exchanger::getRate('eur', 'pln'); // Return Exchanger\ExchangeRate
$rate->getValue(); // Returns rate as float
$rate->getDate()->format('Y-m-d') // Returns exchange date
// Historical rate
Exchanger::getRate('nok', 'sek', now()->subDays(10));
Conversions are expressed fluently, starting the chain with "from" or with "convert":
Exchanger::from('usd')
->to('eur')
->amount(100)
->round(2)
->getValue(); // Converted amount as float
Exchanger::convert()
->from('usd')
->to('eur')
->getValue(); // Amount defaults to 1, so it returns the rate
Historical rates are available through the "date" method (accepts a date string or a DateTimeInterface):
Exchanger::from('nok')->to('sek')->date('2020-01-01')->getValue();
Use "rate" instead of "getValue" in order to obtain the Exchanger\ExchangeRate object:
$rate = Exchanger::from('eur')->to('dkk')->rate();
$rate->getValue();
$rate->getProviderName();
Additional methods:
Exchanger::from('eur')
->to('dkk')
->using(\Exchanger\Service\EuropeanCentralBank::class) // Use only the given services for this conversion
->withoutCache() // Skip the cache for this conversion
->when($someCondition, fn ($c) => $c->round(2)) // Conditional chaining (also "unless")
->getValue();
The "using" and "withoutCache" options only apply to the current conversion; the attached services and cache state are restored afterwards.
The last rate (Exchanger\ExchangeRate) used by a conversion is available afterwards:
Exchanger::getLastExchangeRateResult();
Is sometimes convenient to disable the cache in order of force to request the most recent rate or conversion. In order to achieve that is possible to disable temporally the cache:
Exchanger::setCacheUsage(false); // Cache disabled
Exchanger::setCacheUsage(true); // Cache enabled
Remember that cache is always enabled by default when the configuration key "cache_time" has a valid integer.
It's possible to attach and detach services on demand:
// Detach service
Exchanger::detach(\Exchanger\Service\Cryptonator::class);
// Attach service
Exchanger::attach(\Exchanger\Service\Cryptonator::class);
By default, all the services registered into the configuration are attached by default.
Because this library works as a wrapper for florianv/exchanger it's possible to execute custom queries passing the build query to the "executeQuery" method.
...
Exchanger::executeQuery($query->build());
...