PHPackages                             ogestor/laravel-exchange-rates - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ogestor/laravel-exchange-rates

ActiveLibrary

ogestor/laravel-exchange-rates
==============================

A wrapper package for interacting with the exchangeratesapi.io API.

v3.2.1(5y ago)082MITPHPPHP ^7.2

Since Nov 25Pushed 1y agoCompare

[ Source](https://github.com/ogestor/laravel-exchange-rates)[ Packagist](https://packagist.org/packages/ogestor/laravel-exchange-rates)[ Docs](https://github.com/ash-jc-allen/laravel-exchange-rates)[ RSS](/packages/ogestor-laravel-exchange-rates/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (7)Versions (15)Used By (0)

[![Laravel Exchange Rates](https://camo.githubusercontent.com/775bf4dd462bfd9b3f25e9e6a7ae0ae948911e77b693331ec9f47bf3977d977e/68747470733a2f2f617368616c6c656e64657369676e2e636f2e756b2f696d616765732f637573746f6d2f6c61726176656c2d65786368616e67652d72617465732d6c6f676f2e706e67)](https://camo.githubusercontent.com/775bf4dd462bfd9b3f25e9e6a7ae0ae948911e77b693331ec9f47bf3977d977e/68747470733a2f2f617368616c6c656e64657369676e2e636f2e756b2f696d616765732f637573746f6d2f6c61726176656c2d65786368616e67652d72617465732d6c6f676f2e706e67)

[![Latest Version on Packagist](https://camo.githubusercontent.com/50b3b73978e6265f4fe0002b049f89acbd76410428a3e61e3a0d8935d1b9a9e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f617368616c6c656e64657369676e2f6c61726176656c2d65786368616e67652d72617465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ashallendesign/laravel-exchange-rates)[![Build Status](https://camo.githubusercontent.com/be9fa96790cd2cdc3746b370dc10f3d049040350e78e3cf5cdc4c34fb0cc9f7a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6173682d6a632d616c6c656e2f6c61726176656c2d65786368616e67652d72617465732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/ash-jc-allen/laravel-exchange-rates)[![Total Downloads](https://camo.githubusercontent.com/d0bfe1acd03571bbd866422cf4c63f8eb9d3e56737e9bead411793309377bd7d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f617368616c6c656e64657369676e2f6c61726176656c2d65786368616e67652d72617465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ashallendesign/laravel-exchange-rates)[![PHP from Packagist](https://camo.githubusercontent.com/8824eadae178b1ae231e6d56f1400394a16591462c6dab948e53ddd447f0e79b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f617368616c6c656e64657369676e2f6c61726176656c2d65786368616e67652d72617465733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ashallendesign/laravel-exchange-rates)[![GitHub license](https://camo.githubusercontent.com/bae1da0aba544256b9083286e95974d0802e23349359c69308ad36bd770470da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6173682d6a632d616c6c656e2f6c61726176656c2d65786368616e67652d72617465733f7374796c653d666c61742d737175617265)](https://github.com/ash-jc-allen/short-url/blob/master/LICENSE)

Table of Contents
-----------------

[](#table-of-contents)

- [Overview](#overview)
- [Installation](#installation)
- [Usage](#usage)
    - [Methods](#methods)
        - [Available Currencies](#available-currencies)
        - [Exchange Rate](#exchange-rate)
            - [Getting the Rate Between Two Currencies](#getting-the-rate-between-two-currencies)
            - [Getting the Rate Between More Than Two Currencies](#getting-the-rate-between-more-than-two-currencies)
        - [Exchange Rates Between Date Range](#exchange-rates-between-date-range)
            - [Getting the Rates Between Two Currencies](#getting-the-rates-between-two-currencies)
            - [Getting the Rates Between More Than Two Currencies](#getting-the-rates-between-more-than-two-currencies)
        - [Convert Currencies](#convert-currencies)
            - [Converting Between Two Currencies](#converting-between-two-currencies)
            - [Converting Between More Than Two Currencies](#converting-between-more-than-two-currencies)
        - [Convert Currencies Between Date Range](#convert-currencies-between-date-range)
            - [Converting Between Two Currencies in a Date Range](#converting-between-two-currencies-in-a-date-range)
            - [Converting Between More Than Two Currencies in a Date Range](#converting-between-more-than-two-currencies-in-a-date-range)
    - [Facade](#facade)
    - [Validation Rule](#validation-rule)
    - [Examples](#examples)
    - [Caching](#caching)
        - [Busting Cached Exchange Rates](#busting-cached-exchange-rates)
        - [Preventing Exchange Rates from Being Cached](#preventing-exchange-rates-from-being-cached)
    - [Supported Currencies](#supported-currencies)
- [Testing](#testing)
- [Security](#security)
- [Contribution](#contribution)
- [Credits](#credits)
- [Changelog](#changelog)
- [Upgrading](#upgrading)
- [License](#license)

Overview
--------

[](#overview)

A simple Laravel package used for interacting with the [exchangeratesapi.io](http://exchangeratesapi.io) API. 'Laravel Exchange Rates' allow you to get the latest or historical exchange rates and convert monetary values between different currencies.

Installation
------------

[](#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
-----

[](#usage)

### Methods

[](#methods)

#### Available Currencies

[](#available-currencies)

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

#### Exchange Rate

[](#exchange-rate)

##### Getting the Rate Between Two Currencies

[](#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');

// $result: '1.10086'
```

Note: If a Carbon date is passed as the third parameter, the exchange rate for that day will be returned (if valid). If no date is passed, today's exchange rate will be used.

##### Getting the Rate Between More Than Two Currencies

[](#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 an 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

[](#exchange-rates-between-date-range)

##### Getting the Rates Between Two Currencies

[](#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

[](#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 codes 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

[](#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

[](#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
```

Note: If a Carbon date is passed as the third parameter, the exchange rate for that day will be returned (if valid). If no date is passed, today's exchange rate will be used.

##### Converting Between More Than Two Currencies

[](#converting-between-more-than-two-currencies)

You can also use the `->convert()` method to convert a monetary value from one currency to multiple currencies. To do this, you can pass an array of currency codes strings as the third parameter.

The example below show how to convert £1 'GBP' to 'EUR' and 'USD' at today's exchange rate.

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

// $result: [
//     'EUR' => 110.15884906,
//     'USD' => 125.30569081
// ];
```

#### Convert Currencies Between Date Range

[](#convert-currencies-between-date-range)

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 in a Date Range

[](#converting-between-two-currencies-in-a-date-range)

Similar to getting the exchange rates between a date range, you can also get convert monetary values from one currency to another using the exchange rates. To do this you can use the `->convertBetweenDateRange()` method.

The example below shows how to convert £1 'GBP' to 'EUR' using the exchange rates for the past 3 days.

```
$exchangeRates = new ExchangeRate();
$exchangeRates->convertBetweenDateRange(100, 'GBP', 'EUR', Carbon::now()->subDays(3), Carbon::now());

// $result: [
//     '2020-07-07' => 110.92623405,
//     '2020-07-08' => 111.20625424,
//     '2020-07-09' => 111.53867604,
// ];
```

##### Converting Between More Than Two Currencies in a Date Range

[](#converting-between-more-than-two-currencies-in-a-date-range)

You can also use the `->convertBetweenDateRange()` method to convert a monetary value from one currency to multiple currencies using the exchange rates between a date range. To do this, you can pass an array of currency codes strings as the third parameter.

The example below show how to convert £1 'GBP' to 'EUR' and 'USD' at the past three days' exchange rates.

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

// $result: [
//     '2020-07-07' => [
//         'EUR' => 110.92623405,
//         'USD' => 125.23571825,
//      ],
//     '2020-07-08' => [
//         'EUR' => 111.20625424,
//         'USD' => 125.50737853,
//      ],
//     '2020-07-09' => [
//         'EUR' => 111.53867604,
//         'USD' => 126.50716636,
//      ],
// ];
```

### Facade

[](#facade)

If you prefer to use facades in Laravel, you can choose to use the provided `ExchangeRate ` facade instead of instantiating the `AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate`class manually.

The example below shows an example of how you could use the facade to get the available currencies:

```
