Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

laravel
communitycreator

How to convert one currency to another using Laravel currency

Chinweuba Elijah Azubuike

What is Laravel currency?

Laravel Currency is a Laravel package that gives the historical, latest currency rate and converts from one currency to another. It also provides crypto exchange rates. This is possible with the help of the exchangerate.host APIApplication Programming Interface which is free and does not need an API key.

In this shot, we will focus on how you can convert one currency to another with a practical example.

In a previous shot, I wrote on how to get the latest currency rate using the same Laravel-Currency package, with a practical example.

Check out the package documentation.

Integrating the currency library

To integrate the currency library, we must have the following:

  • PHP >= 7.2

  • Laravel >= 6.0

  • guzzlehttp >= 6.0

Installation

To install the package, we run the command below.


composer require amrshawky/laravel-currency

Converting currency

We chain some methods to get the latest currency:

  • Import the class below into your controller where you want to implement it.

use AmrShawky\LaravelCurrency\Facade\Currency;

In the code below, we convert 150GBP (pounds) to NGN (naira).


Currency::convert()
        ->from('GBP')
        ->to('NGN')
        ->amount(150)
        ->get();

Output

The output of the code will be like so:


6000 // assuming the rate is 400NGN to 1GBP

The code above chains four methods:

  • from(), which is the base currency.
  • to(), which is the quoted currency or the currency you are converting to.
  • amount(), takes an integer value.
  • get(), computes the output.

Parameter

Both the from() and the to() method receive a three letter code representing the currency, e.g., USD, EUR, NGN.

Code

To give a practical example, we will create a controller CurrencyController.php like so:

  • php artisan make:controller CurrencyController.

  • Type this on your command line. You can find your new controller in the app/Http/Controllers/ directory as CurrencyController.php.

<?php
namespace App\Http\Controllers;
use AmrShawky\LaravelCurrency\Facade\Currency;
use Illuminate\Http\Request;

class CurrencyController extends Controller {
    public function ConvertCurrency(Request $request){
        $from = $request->from;
        $to = $request->to;
        $amount= $request->amount;
        $converted=Currency::convert()
            ->from($from) //currncy you are converting
            ->to($to)     // currency you are converting to
            ->amount(100) // amount in USD you converting to EUR
            ->get();

    return view('blade_name',compact('converted'));
    }
}

Explanation

In the code example above, the ConvertCurrency() method will return the $converted amount. The converted amount says 100 USD equals 130 GBP, and it will return 130 or null if the operation fails.

ConvertCurrency() receives the Request façade to enable it to work with the users request, so I assume your blade file contains two input with to and from as their names and finally one input with its name as amount.

RELATED TAGS

laravel
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring