Laravel Create Custom Helper Function

  927 views   6 months ago Laravel

In this article, we will share with you how to create your own custom helpers in laravel application. Laravel provides many excellent helper functions for work with array, string, files, etc.. You can also define your own set of helper functions for your Laravel applications and PHP packages, by using Composer to import them automatically.

Why create own helpers?

Laravel provides many helper functions inbuilt and it's easy to manage our all laravel application. but sometimes it's not enough for us. For eg. you want to convert currency rate in your laravel application. but, laravel is not providing that type helper functions so in this situation, we should create our own helper function in our laravel application and easy to use everywhere.

If you never create your own helper function in laravel application then don't worry here we share with you all step by step and with very easy way.

In this article, we make one custom helper function which return us full name on passing first_name and last_name. this is a very basic idea which we share with you here in this article but, you can doing any login in your custom helper function and manage your laravel application is a very easy way.

Please follow the step to create custom helper functions in laravel application

Step - 1 Create helpers class

First, we need to create one our own helper class and in this class, we will write all our custom helper functions logic. you can create your helper class anywhere in your laravel application it depends on you but here are a few suggested locations:

app/helpers.php
app/Http/helpers.php

Here we are the most preferred app/Http/helpers.php location. so after creating this file you just put one custom helper function in this helpers file.

<?php

function getUserFullName($id) {
	$data = \DB::table('users')
        ->select(\DB::raw('CONCAT(firstname, " ", lastname) as username'))
        ->where('id', $id)
        ->first();

    return $data->username;
}

Step - 2 Add helpers file in composer.json file

Now next step after done create one custom helpers function in app/Http/helpers.php file then you must be make this helpers file autoload. so, you can use that all helpers in everywhere in your laravel applications. open your composer.json file and make the following changes in it.

"autoload": {
    "files": [
        "app/Http/helpers.php"
    ],
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

After adding app/Http/helpers.php a file in autoload section then you should run once following composer command in your terminal or CMD.

composer dump-autoload

How to use custom helper functions in Controller

After done above two steps then now we will check our custom helpers function will work perfectly or not. Here you can see you can use directly your custom helpers function in your any controller files.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

class HomeController extends Controller
{   
    
    public function index()
    {
        $fullusername = getUserFullName(Auth::user()->id);
        dd($fullusername);
    }

}

How to use custom helper function in blade file

You can also use your custom helper function in your any blade file like that.

@extends('layouts.app')

@section('content')

@php
	$fullusername = getUserFullName(Auth::user()->id);
    dd($fullusername);
@endphp

@endsection

Conclusion

As you can see, create custom helper functions in laravel application is very easy. and it is more helpfull to manage your laravel application code.

We are hope this tutorials help everyone.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]