PHPackages                             ez-laravel/currencies - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ez-laravel/currencies

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ez-laravel/currencies
=====================

Easy Laravel Currencies &amp; Convertion

v1.0.0(5y ago)08MITPHP

Since Jun 24Pushed 5y ago1 watchersCompare

[ Source](https://github.com/ez-laravel/currencies)[ Packagist](https://packagist.org/packages/ez-laravel/currencies)[ RSS](/packages/ez-laravel-currencies/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

EZ Laravel Currencies
=====================

[](#ez-laravel-currencies)

This package provides your application with the functionality to use currencies and easily retrieve conversion rates and apply them to prices.

It makes use of external API's, some of which require an API key. You'll find more details in the instructions below.

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

[](#installation)

Run the following command in your project directory to install the package:

```
composer require ez-laravel/currencies

```

Publish the migrations and seeds using the following command:

```
php artisan vendor:publish --provider="EZ\Currencies\Providers\CurrenciesServiceProvider" --tag=database

```

Run the migrations with:

```
php artisan migrate

```

Update your `DatabaseSeeder.php` class to load the new seeder and afterwards run the following commands to seed your database:

```
composer dumpautoload
php artisan db:seed

```

Run the following command to (optionally) publish the package's config file:

```
php artisan vendor:publish --provider="EZ\Currencies\Providers\CurrenciesServiceProvider" --tag=config

```

Configuration
-------------

[](#configuration)

#### Default currency

[](#default-currency)

The default currency is set to EUR, which you can change by adding the following key to your `.env` file with the desired currency code:

```
CURRENCIES_DEFAULT=USD

```

#### Currency Conversion Rate API

[](#currency-conversion-rate-api)

The following APIs are supported or under development to be supported:

- [Fixer.io](https://fixer.io)
- [Ratesapi.io](https://ratesapi.io/)
- [Frankfurter.app](https://www.frankfurter.app)
- [Exchangeratesapi.io](https://exchangeratesapi.io/)

You can change the drive the package will use by adding the following key to your `.env` file:

```
CURRENCIES_API_DRIVER=fixer

```

#### Fixer

[](#fixer)

`fixer`

Get an API key from the [fixer.io website](https://fixer.io) and add it to your `.env` file:

```
CURRENCIES_FIXER_API_KEY=xxxxxxxx

```

#### RatesAPI

[](#ratesapi)

`rates`

The [ratesapi.io website](https://ratesapi.io) does not require an API key!

#### Frankfurter

[](#frankfurter)

`frankfurter`

The [frankfurter.app website](https://frankfurter.app) does not require an API key!

#### ExchangeRatesAPI

[](#exchangeratesapi)

`exchangerates`

The [exchangeratesapi.io website](https://exchangeratesapi.io/) does not require an API key!

Usage
-----

[](#usage)

#### Available methods

[](#available-methods)

```
// Currency getters
$currencies = Currencies::getAll();
$currency = Currencies::find($id);
$currency = Currencies::findBy($field, $value);
$currency = Currencies::findByCode($code);
$num_currencies = Currencies::countAll();

// Preloaded currency getters
$currencies = Currencies::getAllPreloaded();
$currency = Currencies::findPreloaded($id);
$currency = Currencies::findPreloadedBy($field, $value);

// Currency conversion rate getters
$conversionRates = Currencies::getConversionRates();
$conversionRates = Currencies::getConversionRatesByCode($code);

// Update all currency conversion rates
Currencies::updateConversionRates();

// Conversion methods
$convertedValue = Currencies::convert($fromCurrency, $toCurrency, $x);
$convertedValues = Currencies::convertToAll($fromCurrency, $x);
```

#### Updating conversion rates

[](#updating-conversion-rates)

When you've just installed the package you should perform the following command to manually update all of your currencies conversion rates:

```
php artisan currencies:update-conversion-rates

```

To keep the conversion rates up-to-date automatically schedule the above command to be ran every day (or at whatever interval you'd like) by updating your `app/Console/Kernel.php` file to include the following:

```
protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('currencies:update-conversion-rates')->daily();
}
```

[More information on task scheduling can be found here](https://laravel.com/docs/7.x/scheduling#scheduling-artisan-commands).

#### Extending the Currency model

[](#extending-the-currency-model)

In most applications you will want to create relationships between the Currency model and for example a Product model or Order model. To do this simply create your own Currency model which extends the `EZ\Currencies\Models\Currency` model and update the model path in the `currencies.php` config file.

So for example:

```
