How to create custom helpers in laravel6

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:


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.


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

    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": [
    "classmap": [
    "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.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

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


How to use custom helper function in blade file

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



	$fullusername = getUserFullName(Auth::user()->id);



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. if you have any issues or question reagarding create create custom helper functions in laravel. so, please comment bellow. Thanks..