restuniverse/currency

This package provides Laravel functions such as currency formatting and conversion using up-to-date exchange rates.

v2.3 2022-08-01 06:03 UTC

This package is auto-updated.

Last update: 2024-04-25 09:49:25 UTC


README

Installation

Composer

From the command line run:

$ composer require restuniverse/currency

Laravel's >=5.5 Auto-Discovery

Simply install the package and let Laravel do its magic.

Manual Setup

Once installed you need to register the service provider with the application. Open up config/app.php and find the providers key.

'providers' => [

    \RestUniverse\Currency\CurrencyServiceProvider::class,

]

This package also comes with a facade, which provides an easy way to call the the class. Open up config/app.php and find the aliases key.

'aliases' => [

    'Currency' => \RestUniverse\Currency\Facades\Currency::class,

];

Publish the configurations

Run this on the command line from the root of your project:

php artisan vendor:publish --provider="RestUniverse\Currency\CurrencyServiceProvider" --tag=config

A configuration file will be published to config/currency.php.

Migration

If currencies are going to be stored in the database.

php artisan vendor:publish --provider="RestUniverse\Currency\CurrencyServiceProvider" --tag=migrations

Run this on the command line from the root of your project to generate the table for storing currencies:

$ php artisan migrate

note: Add your API_KEY to the .env file with (REST_UNIVERSE_API_KEY).

Basic usage

The simplest way to use these methods is through the helper function currency() or by using the facade. For the examples below we will use the helper method.

currency($amount, $from = null, $to = null, $format = true)

Arguments:

$amount - The float amount to convert $from - The current currency code of the amount. If not set, the application default will be used (see config/currency.php file). $to - The currency code to convert the amount to. If not set, the user-set currency is used. $format - Should the returned value be formatted.

Usage:

echo currency(12.00);               // Will format the amount using the user selected currency
echo currency(12.00, 'USD', 'EUR'); // Will format the amount from the default currency to EUR

Updating Exchange Rates

Update exchange rates from restuniverse.com. An API key is needed to use Rest Universe. Add yours to the config file.

php artisan currency:hydrate

Security

If you discover any security related issues, please email security@restuniverse.com instead of using the issue tracker.