Currency Converter is a simple easy to use package for Laravel for currency conversion.
Install Currency Converter via composer. Note: If you do not have composer yet, you can install it by following the instructions on https://getcomposer.org
composer require amkas/currency-converter
in bootstrap/providers.php you can add following line
\Amkas\CurrencyConverter\ConversionServiceProvider::class,
To publish the assets run the below command.
php artisan vendor:publish --tag=amkas-currency-converter
Or
php artisan vendor:publish --provider="Amkas\CurrencyConverter\ConversionServiceProvider"
This command will copy three files as below:
Copying file [amkas\currency-converter\src\Models\CurrencyRate.php] to [app\Models\CurrencyRate.php] DONE
Copying file [amkas\currency-converter\src\config\currency_converter.php] to [config\currency_converter.php] DONE
Copying directory [amkas\currency-converter\src\database\migrations] to [database\migrations] DONE
So there are three files
- Currency rates migration
- Currency Rate model
- config/currency-converter.php
After that, you need to run the migration command to migrate the currency rates table into database as below:
php artisan migrate
Then you can set the default currency in config file as below:
'default_currency' => 'USD',
and other config settings.
After that, you can create a CRUD to save the currency rates into database
In Controller include Currency Facade and call as below:
use Amkas\CurrencyConverter\Facades\Currency;
$convertedAmount = Currency::convertAmount(10, 'EUR');
If you want to use a helper's function use below.
$convertedAmount = convertAmount(10, 'EUR');
If you want to convert amount from one currency to another using Currency Facade, call chain functions as below:
$convertedAmount = Currency::amount(10)
->from('EUR')
->to("USD")
->convert();
with helper functions you can call as below:
$convertedAmount = amount(10)
->from("EUR")
->to("USD")
->convert();
Note: Since this currency converter has cache implemented to avoid database query everytime when currency conversion function will call, so every time when new currency rate will add via CurrencyRate model, the cahce will automatically reset.
However, you can reset that specific cache by runing the below command:
php artisan converter:reset-cache