Laravel 9 - Create Custom Helper File Tutorial

Apr 16, 2022 . Admin

Hi Dev,

In this tute, we will discuss laravel 9 create a custom helper file example . if you have question about How to create custom helper functions in Laravel 9 then I will give simple example with solution. I’m going to show you about laravel 9 creating a global function . you'll learn Create own custom helper functions / Classes in Laravel.

today, we are going to create a helper function in laravel 9, this is the same method in creating a helper function in laravel 9. Helper function helps us to create a function that can be called anywhere in our app. That is a global function that can be called both in the views and also in the controller.

This article is focused on Laravel provides many excellent helper functions that are convenient for doing things like working with arrays. This article will give you simple example of file paths. step by step explain strings. we will help you to give example of and routes. Follow bellow tutorial step of among other things like the beloved dd() function..

You can also define your own set of helper functions for your Laravel applications and PHP packages, by using Composer to import them automatically.

In laravel custom helper, you can create your own function and call anywhere like route, blade view, models, controller etc in laravel project. It is best practice to code reusable and saves a lot of time to replicate the code.

So let's start by following an example.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Helper File

In this step, you need to create app/Helpers/helpers.php in your laravel project and put the following code in that file:

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

In this step, you have to put the path of the helpers file, so basically open the composer.json file and 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 register, we need to run the composer auto load command so that will load our helper file.

Then after run bellow command:

composer dump-autoload
Step 4: Create Route

Next, You have to open and 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('run-helper', function(){
    $ymd = convertDmyToYmd('02-12-2022');
    var_dump("Converted into 'YMD': " . $ymd);
    
    echo "<br>";

    $mdy = convertYdmToMdy('2022-02-12');
    var_dump("Converted into 'MDY': " . $mdy);
});
Step 5: Use In Blade File:

You can also use in blade file as like bellow:

Date: {{ convertDmyToYmd('2022-02-12') }}

Date: {{ convertYdmToMdy('02-12-2022') }}

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/run-helper
Output:
string(32) "Converted into 'YMD': 2022-12-02"
string(32) "Converted into 'MDY': 12-02-2022"
#Laravel 9