Custom Helper function in Laravel with real Example

Share with friends

Custom Helper function in Laravel with real Example. Laravel is the most used PHP framework because it provides the best possible functionality for rapid web development and its customizable behavior. you can create a custom global helper in laravel. today we are going to show you how to create Custom Helper function in Laravel with real Example.

after that, you can call this helper function anywhere in your laravel project. there are lots of predefined helper function you can find these on laravel official documentation. predefined helpers might not fulfill all our your requirement so you might need Costume helper function.

Create a Laravel project

I am assuming that you already installed laravel with the help of the composer. run the following command in your terminal to create a new laravel project.

laravel new custome_helper

Or you can install with composer

 composer create-project --prefer-dist laravel/laravel custome_helper

then rename your .env example file to .env file.

Create Helper File to create a custom helper function

You can create a helper file under the app folder. I am creating my helper file under app/Helpers then named the file testHelper.php then create a function called test.

<?php
function h_testHelper($firstName, $lastName){
    $fullName = '';
    if(($firstName)&& ($lastName)){
        $fullName = $lastName . $firstName;
    }
    return $fullName;
}
?>

Copy and then paste the above code in your helper file. I am initializing my helper function with h.

Create a Provider

create a provider to make our helper globally available. run the following artisan command to create a new provider.

php artisan make:provider HelperServiceProvider

you can find this provider file under app/Providers. copy and paste the following code in your Provider.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class HelperServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
        foreach (glob(sprintf('%s/Helpers/*.php', app_path())) as $helper_file) {
            require_once($helper_file);
        }
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Add Provider to Config

add your provider to the config app file. open your config/app.php file and there you can find providers array.at the bottom of the array add the following line

App\Providers\HelperServiceProvider::class,

Call From Controller

You can call the helper function in a controller in the following way.

<?php

namespace App\Http\Controllers;


class testcontroller extends Controller
{
    public function testhelpercall(){
        $datas =[
            [
                'id'=>1,
                'firstname'=> 'thierry',
                'familyname'=> 'henry',

            ],
             [
                'id'=>2,
                'firstname'=> 'myfirst',
                'familyname'=> 'mylastname',

            ]
        ];
        foreach($datas as $user)
        {
            $user = (object) $user;
            $datas = [];
            $datas['id'] = $user->id;
            $datas['full_name'] = h_fullName($user);
            $user_data[]=$datas;
        }
        return $user_data;
    }
}

The Output of the above controller is as below

Call from View(Blade)

you can call helper in the blade template in the following way.

<br/>
dear {{ h_fullName($staffdetail) }}
<br/><br/>

I am sending you the guest detail of our monday conference<br/>


<br/>
@foreach ($guests as $guest)
 Name.:{{ h_fullName($guest) }}<br/>

 Address:{{ $guest->address  }}<br>

 Phone Number:{{ $guest->tel  }}<br>

<br/>

@endforeach
<br/>

Here I have presented a very simple example but you can do some serious complex tasks in helpers like getting data from a database storing data to the database and many more.

we also have other article related to laravel you can check those article from here