Language:

Search

Creating Custom Helper Functions in Laravel: Enhance Your Development Efficiency with Unique Helpers

  • Share this:
Creating Custom Helper Functions in Laravel: Enhance Your Development Efficiency with Unique Helpers

Laravel allows you to create your own helper functions easily. Here's how to create a custom helper function in Laravel:

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

Step 1: Install Laravel

composer create-project laravel/laravel example-app

Step 2: Create a new file

Create a new file in the app directory of your Laravel project. Name this file whatever you want, but make sure it has the .php extension. For example, you can name it helpers.php

Step 3: Define your helper function

Define your helper function
In your new file, define your helper function. Here's an example:

<?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');
    }
}


if (!function_exists('myHelperFunction')) {
    function myHelperFunction($param1, $param2) {
        // Your code here
    }
}

In this example, we're checking if the function myHelperFunction doesn't exist yet (using function_exists), and then defining the function. You can replace myHelperFunction with the name of your own function, and replace the parameters and code inside the function with your own logic.

 

Step 4: Load the helper file 

To use your helper function in your Laravel project, you need to load the file you just created. To do this, open the composer.json file in the root directory of your Laravel project, and add the following code to the autoload section:

 

        "files": [
            "app/helpers.php"
        ]

This tells Laravel to load the helpers.php file when the application starts up.

 

Step 5: Refresh Composer autoload

After adding the files section to the autoload section in the composer.json file, you need to refresh the Composer autoload by running the following command in your terminal:

composer dump-autoload

Step 6: Use your helper function

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    // Using the helper function in a controller
    public function myControllerMethod() {
        $result = myHelperFunction($param1, $param2);
        // Your code here
    }
}

 

Now you can use helper function in blade:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Import Excel or CSV file in Laravel</title>
    </head>
    <body>
        <p>Date: {{ convertYmdToMdy('2022-02-12') }}</p>
        <p>Date: {{ convertMdyToYmd('02-12-2022') }}</p>
    </body>
    </html>

 

Step 7: Add helper function in route

 

<?php
 
use Illuminate\Support\Facades\Route;
 
 
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);
});

 

 

 


Jahangir Alam

Jahangir Alam

I'm a full-stack website developer with a wealth of experience in creating beautiful and functional websites. I have a strong foundation in web development and am well-versed in a variety of programming languages, including HTML, CSS, JavaScript, and PHP.
With over 3 years of experience in the industry, I have worked on a wide range of projects, from small startup websites to large enterprise-level applications. He has a proven track record of delivering high-quality results, on time and within budget.
I am comfortable working with various web development frameworks such as React, Angular, and Vue.js. He has strong knowledge of the latest web development trends and best practices, which he uses to create responsive and user-friendly websites.