Blog Detail

19

Sep
Get Latest or Historical Exchange Rates & Convert in Laravel cover image

arrow_back Get Latest or Historical Exchange Rates & Convert in Laravel

Laravel Exchange Rate is a simple Laravel package used for interacting with the exchangeratesapi.io API. ‘Laravel Exchange Rates’ allows you to get the latest or historical exchange rates and convert monetary values between different currencies.

Installation

You can install the package via Composer:

composer require ashallendesign/laravel-exchange-rates

The package has been developed and tested to work with the following minimum requirements:

  • PHP 7.2
  • Laravel 6

Usage

Available Currencies

$exchangeRates = new ExchangeRate();
$exchangeRates->currencies();

Exchange Rate

Getting the Rate Between Two Currencies

To get the exchange for one currency to another, you can use the ->exchangeRate() method. When doing this, you can pass the currency code as a string as the second parameter. The ->exchangeRates() method will then return a string containing the exchange rate.

The example below shows how to get the exchange rate from ‘GBP’ to ‘EUR’ for today.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRate('GBP', 'EUR');

Getting the Rate Between More Than Two Currencies

It is possible to get the exchange rates for multiple currencies in one call. This can be particularly useful if you are needing to get many exchange rates at once and do not want to multiple API calls.

To do this, you can use ->exchangeRate() method and pass an array of currency code strings as the second parameter. This will return the array containing the exchange rates as strings.

The example below shows how to get the exchange rates from ‘GBP’ to ‘EUR’ and ‘USD’ for today.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRate('GBP', ['EUR', 'USD']);

// $result: [
//     'EUR' => '1.10086',
//     'USD' => '1.25622'
// ];

Exchange Rates Between Date Range

Getting the Rates Between Two Currencies

To get the exchange rates between two currencies between a given date range, you can use the ->exchangeRateBetweenDateRange() method. When doing this, you can pass the currency code as a string as the second parameter. The method will then return an array containing the exchange rates.

The example below shows how to get the exchange rates from ‘GBP’ to ‘EUR’ for the past 3 days.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRateBetweenDateRange('GBP', 'EUR', Carbon::now()->subWeek(), Carbon::now());

// $result: [
//     '2020-07-07' => 1.1092623405
//     '2020-07-08' => 1.1120625424
//     '2020-07-09' => 1.1153867604
// ];

Getting the Rates Between More Than Two Currencies

To get the exchange rates for multiple currencies in one call, you can pass an array of currency code strings as the second parameter to the
->exchangeRateBetweenDateRange() method.

The example below shows how to get the exchange rates from ‘GBP’ to ‘EUR’ and ‘USD’ for the past 3 days.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->exchangeRateBetweenDateRange('GBP', ['EUR', 'USD'], Carbon::now()->subDays(3), Carbon::now());

// $result: [
//     '2020-07-07' => [
//         'EUR' => 1.1092623405,
//         'USD' => 1.2523571825,
//      ],
//     '2020-07-08' => [
//         'EUR' => 1.1120625424,
//         'USD' => 1.2550737853,
//      ],
//     '2020-07-09' => [
//         'EUR' => 1.1153867604,
//         'USD' => 1.2650716636,
//      ],
// ];

Convert Currencies

When passing in the monetary value (first parameter) that is to be converted, it’s important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).

Converting Between Two Currencies

Similar to how you can get the exchange rate from one currency to another, you can also convert a monetary value from one currency to another. To do this you can use the ->convert() method.

The example below shows how to convert £1 ‘GBP’ to ‘EUR’ at today’s exchange rate.

$exchangeRates = new ExchangeRate();
$result = $exchangeRates->convert(100, 'GBP', 'EUR', Carbon::now());

// $result: 110.15884906

This package has a lot more features with code examples, If you wanna dig more please visit Github.

Published at : 19-09-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project