Laravel 6.2 Custom Helper Tutorial with Example
In this article, we are going to learn how to make a custom helper in Laravel 6.2. We can define many custom functions in the helper and we can use the functions anywhere we want.
Let’s follow these steps to create a custom helper:
Table of Contents
Step 1 : Create helpers.php File
Navigate to app
folder and create a file called “helpers.php‘. We are going to create a currency convert function in the custom helper. Paste the below code in the helper.
<?php
function convertCurrency($amount,$from_currency,$to_currency){
$apiKey = 'your-api-key-here';
$from_Currency = urlencode($from_currency);
$to_Currency = urlencode($to_currency);
$query = "{$from_Currency}_{$to_Currency}";
// change to the free URL if you're using the free version
$json = file_get_contents("https://api.currconv.com/api/v7/convert?q={$query}&compact=ultra&apiKey={$apiKey}");
$obj = json_decode($json, true);
$val = floatval($obj["$query"]);
$total = $val * $amount;
return number_format($total, 2, '.', '');
}
Step 2 : Define Helper in composer.json
Open the composer.json file and in the autoload section we need to define the location of helpers.php. Let’s define:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/helpers.php"
]
},
After adding the helpers.php, run this command to load the file globally in our project:
composer dump-autoload
Step 3 : Use in Controller
We have created the custom helper. Let’s call from the controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Response;
class TestController extends Controller
{
public function index()
{
$converted_amount = convertCurrency(10, 'USD', 'GBP');
echo $converted_amount;
}
}
Step 4 : Use in Blade File
As the controller, we can also call helper functions in the view blade file. Here’s an example:
@extends('layouts.app')
@section('content')
@php
$converted_amount = convertCurrency(10, 'USD', 'GBP');
echo $converted_amount;
@endphp
@endsection
We have created and tested the custom helper. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)