How to Create Custom Helper Functions in Laravel 9 ?

Here today now in this post i will show you How to Create Custom Helper Functions in Laravel 9 ? So here I would like to show you an example of laravel 9 create the custom helper functions. Now in this post i will give you a simple example about laravel 9 creating a global function. So if you are look at an example of laravel 9 create a custom helper file. Then Here you will learn about laravel 9 to create a global helper file.

We know that Laravel 9 also provides us the helper functions for array, URL, route, path, etc. but sometimes we may need to more custom helper functions on our application. so easily we can create our own custom helper file and define as a global functions and also that can easily use it.

Here, I will give you an example also with few steps to show you how we can create a custom helper function in laravel 9 appliaction.

Step 1: Install Laravel 9

If you have already laravel 9 application then this step is not need ; however, if you don’t have then need to created the laravel application, then you may go ahead and need execute the below command:

composer create-project laravel/laravel example-app
Step 2: Create helpers.php File

Now in this step, we need to create the app/Helpers/helpers.php in our laravel project and then put the following code in that file:

app/Helpers/helpers.php

<?php
  
use Carbon\Carbon;
  
/**
 * Write code on Method
 *
 * @return response()
 */
if (! function_exists('convertYmdToMdy')) {
    function convertYmdToMdy($date)
    {
        return Carbon::createFromFormat('Y-m-d', $date)->format('m-d-Y');
    }
}
  
/**
 * Write code on Method
 *
 * @return response()
 */
if (! function_exists('convertMdyToYmd')) {
    function convertMdyToYmd($date)
    {
        return Carbon::createFromFormat('m-d-Y', $date)->format('Y-m-d');
    }
}
Step 3: Register File Path In composer.json File

Now in this step, we need to put path of helpers file,so basically need to open composer.json file and then put the following code in that file:

composer.json

...
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/,
    },
,
    "files": [
        "app/Helpers/helpers.php"
    ]
},
...

After the register, we need to run the composer auto load command so that will load the our helper file.

Then in the next need run bellow command:

composer dump-autoload
Step 4: Add Route

Next, we need to open and then update the following routes in the routes/web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
/*
|--------------------------------------------------------------------------
| 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('call-helper', function(){
  
    $mdY = convertYmdToMdy('2022-02-12');
    var_dump("Converted into 'MDY': " . $mdY);
    
    $ymd = convertMdyToYmd('02-12-2022');
    var_dump("Converted into 'YMD': " . $ymd);
});

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →