DEV Community

Cover image for How to Create Custom Helper Functions in Laravel 12
Msh Sayket
Msh Sayket

Posted on

How to Create Custom Helper Functions in Laravel 12

In this example, i will show How to Create Custom Helper Functions in Laravel 12 application.

We know Laravel 12 also provides helper functions for arrays, URLs, routes, paths, etc. But sometimes, we may require more custom helper functions for our project. So, we need to create our own custom helper file and define global functions that can be easily used.

How to Create Custom Helper Functions in Laravel 12

Step 1: Install Laravel 12

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

laravel new example-app
Enter fullscreen mode Exit fullscreen mode

Step 2: Create helpers.php File

In this step, you need to create app/Helpers/helpers.php in your Laravel project and place 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');
    }
}
Enter fullscreen mode Exit fullscreen mode

Read Also: How to generate pdf file in laravel 12 using dompdf Example

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"
        ]
    },

...
Enter fullscreen mode Exit fullscreen mode

After registering, we need to run the composer autoload command so that it loads our helper file. Next, run the command below:

composer dump-autoload
Enter fullscreen mode Exit fullscreen mode

Step 4: Add Route

Next, you have to open and update the following routes in the routes/web.php file.

Read More

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someoneโ€™s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay