Laravel Currency Converter Package Example

Sep 10, 2022 . Admin



Hello Friends,

Here, I will show you laravel currency converter package example. step by step explain get current and historical currency exchange rates in laravel. This article goes in detail on how to convert one currency to another using laravel currency. I would like to show you how to fetch currency exchange rates in laravel. Let's see below an example of how to get currency exchange rates in laravel.

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 exchange rate.host API which is free and does not need an API key.

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Step 2: Integrating the currency library

To install the package, we run the command below.

composer require amrshawky/laravel-currency
Step 3: Converting currency

In the code below, we convert One Currency to Other Currency

<?php

namespace App\Http\Controllers;

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

class DemoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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: Create Slider Route 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('/', function () {
    return view('welcome');
});

Route::get('/currency',[DemoController::class,'index'])->name('currency');
Step 5: Create Currency Index View

let's create Currency.blade.php(resources/views/currency.blade.php) for the form and we will write the design code here and put the following code:

currency.blade.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <title>How to Fetch Currency Exchange Rates in Laravel?</title>
    <style>
        body{
            background-color: #d2d2d2;
        }
        form{
            background-color: #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 m-5 p-5" method="get">
                    @csrf
                    <div class="row">
                        <div class="col-md-12 mb-4">
                            <div class="row">
                                <h4 class="col-md-7 ps-2 m-0">Fetch Currency Exchange Rates in Laravel</h4>
                                <strong class="col-md-5 p-0 m-0 text-end" style="color: #008B8B;">NiceSnippets.com</strong>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="row">
                                <div class="col-md-5 mb-3">
                                    <select class="form-select" name='currency_from' aria-label="Default select example" required>
                                        <option value="" @if (Request::get('currency_from') == null) none @endif>Select Currency</option>
                                        <option value="AUD" @if (Request::get('currency_from') == 'AUD')? selected : none @endif>Australia Dollar</option>
                                        <option value="EUR" @if (Request::get('currency_from') == 'EUR')? selected : none @endif>Euro</option>
                                        <option value="GBP" @if (Request::get('currency_from') == 'GBP')? selected : none @endif>Great Britain Pound</option>
                                        <option value="INR" @if (Request::get('currency_from') == 'INR')? selected : none @endif>India Rupee</option>
                                        <option value="JPY" @if (Request::get('currency_from') == 'JPY')? selected : none @endif>Japan Yen</option>
                                        <option value="USD" @if (Request::get('currency_from') == 'USD')? selected : none @endif>USA 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' aria-label="Default select example" required>
                                        <option value="" @if (Request::get('currency_to') == null) none @endif>Select Currency</option>
                                        <option value="AUD" @if (Request::get('currency_to') == 'AUD')? selected : none @endif>Australia Dollar</option>
                                        <option value="EUR" @if (Request::get('currency_to') == 'EUR')? selected : none @endif>Euro</option>
                                        <option value="GBP" @if (Request::get('currency_to') == 'GBP')? selected : none @endif>Great Britain Pound</option>
                                        <option value="INR" @if (Request::get('currency_to') == 'INR')? selected : none @endif>India Rupee</option>
                                        <option value="JPY" @if (Request::get('currency_to') == 'JPY')? selected : none @endif>Japan Yen</option>
                                        <option value="USD" @if (Request::get('currency_to') == 'USD')? selected : none @endif>USA Dollar</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="amount" class="form-label">Amount</label>
                                        <input type="number" class="form-control" id="amount" value="{{ Request::get('amount') }}" name="amount" required>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="date" class="form-label">Date</label>
                                        <input type="date" class="form-control" name='date' value="{{ Request::get('date') }}" id="date">
                                        <span class=" d-block">
                                            <i class="fa fa-calendar"></i>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="mb-3">
                                    <label for="amount" class="form-label">Converted Amount</label>
                                    <input type="number" class="form-control" value="{{ $converted }}" id="amount" name="amount" disabled>
                                </div>
                            </div>                              
                            <div class="col-md-12 d-flex justify-content-center mt-4">
                                <button type="submit" class="col-3 btn btn-primary">Submit</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>      
    </div>
</body>
</html>
Step 6: Start Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

http://localhost:8000/currency
Output:

I hope it can help you...

#Laravel