How To Exchange Currency Rates In Laravel Example

  • 16-09-2022
  • 2983
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

How to convert one currency to another using laravel currency ?; in this comprehensive tutorial, we will integrate currency exchange rates from one currency to another currency. For that, we need to install a composer to get the latest updated currency foreign rate.

When your site working worldwide and accepting more than one current that time you need just need exchange and need to show location or country base currency. At that time you need to convert your local currency based on location and country currency conversion.

So we will integrate currency exchange or fetch the current rate of the currency base on the selected currency. Getting the latest currency given currency rate below the given composer will help us to fetch it.

So let's start integrating the latest currency exchange rate in a laravel app. by starting install composer "composer require amrshawky/laravel-currency".

Preview:
image

Step 1. Composer Install

Install laravel composer currency in the application. use the below-provided command in your project terminal and install composer for currency exchange rate gets.

composer require amrshawky/laravel-currency

Step 2. Route Define

Create a controller, we will create a demo controller using the below command in the project terminal.

php artisan make:controller DemoController

Import created controller in the web route file. Define a route for getting currency exchange rate based on selected in the form.

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('currency',[DemoController::class,'index'])->name('currency');

Step 3. Demo Controller

Import the Currency class in the demo controller. define route function here. and paste the given code into your controller method. This method will help us to change the rate from one currency to another currency.

This Currency class returns your current rate. pass to the blade file to getting view it and compare it.

app/Http/Controllers/DemoController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use AmrShawky\LaravelCurrency\Facade\Currency;

class DemoController extends Controller
{
     public function index(Request $request)
     {
          $converted = '';

          if ($request->filled('currency_from')) {
               
               $convertedObj = Currency::convert()
                         ->from($request->get('currency_from'))
                         ->to($request->get('currency_to'))
                         ->amount($request->get('amount'));

               if($request->filled('date')){

                    $convertedObj = $convertedObj->date($request->get('date'));
               }

               $converted = $convertedObj->get();
          }

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

Step 4. View Blade File

Blade file for showing the form, two input filed dropdown. one dropdown for the original currency select. while another dropdown for the compare currency. select and compare it. just copy the given code and paste it into your blade file.

resources/views/currency.blade.php
<!doctype html>
<html lang="en">
     <head>
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title>Currency Covert Example - Webappfix</title>
     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">

     <style type="text/css">
          body{
          background: #d2d2d2;
          }
          form{
          background: #fff;
          }
     </style>
     </head>
     <body>
          <div class="container">
          <div class="row">
          <div class="col-md-12 mt-5 pt-5">
               <form action="{{ route('currency') }}" class="border rounded mt-5 pt-5" method="get">
               <div class="row">
                    <div class="col-md-12 mb-4 text-center">
                    <h4 class="col-md-12 py-2 m-0">Fetch Currency Exchange Rates In Laravel - Webappfix</h4>
                    </div>

                    <div class="col-md-12">
                    <div class="row">
                    <div class="col-md-5 mb-3">
                         <select class="form-select" name="currency_from" required>
                         <option value="">Select Currency</option>
                         <option value="AUD" {{ Request::get('currency_from') == 'AUD'?'selected':'' }}>Australia Dollar</option>
                         <option value="EUR" {{ Request::get('currency_from') == 'EUR'?'selected':'' }}>Euro</option>
                         <option value="GBP" {{ Request::get('currency_from') == 'GBP'?'selected':'' }}>Great Britin Pound</option>
                         <option value="INR" {{ Request::get('currency_from') == 'INR'?'selected':'' }}>India Rupee</option>
                         <option value="JPY" {{ Request::get('currency_from') == 'JPY'?'selected':'' }}>Japan Yen</option>
                         <option value="USD" {{ Request::get('currency_from') == 'USD'?'selected':'' }}>USD Dollar</option>
                         </select>
                    </div>
                    <h4 class="col-md-2 text-center m-0 p-0">To</h4>
                    <div class="col-md-5 mb-3">
                         <select class="form-select" name="currency_to" required>
                         <option value="">Select Currency</option>
                         <option value="AUD" {{ Request::get('currency_to') == 'AUD'?'selected':'' }}>Australia Dollar</option>
                         <option value="EUR" {{ Request::get('currency_to') == 'EUR'?'selected':'' }}>Euro</option>
                         <option value="GBP" {{ Request::get('currency_to') == 'GBP'?'selected':'' }}>Great Britin Pound</option>
                         <option value="INR" {{ Request::get('currency_to') == 'INR'?'selected':'' }}>India Rupee</option>
                         <option value="JPY" {{ Request::get('currency_to') == 'JPY'?'selected':'' }}>Japan Yen</option>
                         <option value="USD" {{ Request::get('currency_to') == 'USD'?'selected':'' }}>USD Dollar</option>
                         </select>
                    </div>
                    </div>
                    </div>
                    <div class="col-md-12">
                    <div class="row">
                    <div class="col-md-6 mb-3">
                         <label>Amount</label>
                         <input type="number" name="amount" class="form-control" value="{{Request::get('amount')}}" required>
                    </div>
                         <div class="col-md-6 mb-3">
                         <label>Date</label>
                         <input type="date" name="date" class="form-control" value="{{Request::get('date')}}" required>
                    </div>
                    </div>
                    </div>
                    <div class="col-md-12">
                    <div class="mb-3">
                    <label>Converted Amount</label>
                    <input type="number" name="amount" value="{{$converted}}" disabled>
                    </div>
                    </div>
                    <div class="col-md-12 d-flex justify-content-center mb-4">
                    <button type="submit" class="col-3 btn btn-primary">Submit</button>
                    </div>
               </div>
               </form>
          </div>
          </div>
          </div>
     </body>
</html>

Step 5. Start Server

Start the development server using the below command in your project terminal.

php artisan serve

Run this URL in your browser and try your currency exchange to another currency.

http://127.0.0.1:8000/currency
Conclusion:

We installed a currency exchange composer in the application, as well as we created a demo controller for getting the current currency exchange rate. for more practice below also given a youtube video for more practical. all the guidelines are given in this tutorial.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :