Create Helper Function in Laravel

armanrahman

Arman Rahman

Posted on June 19, 2023

Create Helper Function in Laravel

Sometimes we need to create a helper function on the Laravel project to use a function on the whole project.

To create a helper function, we need to create a helper file in the following folder -

app\helpers\helpers.php

on that folder write your function like this -

<?php

if (!function_exists('abc')) {
    function abc($parameter)
    {
        // statement 
        return xyz;
    }
}
Enter fullscreen mode Exit fullscreen mode

after that we need to add this folder in autoloader on -

composer.json

"autoload": {
        "psr-4": {
            ///
        },
        "files":[
            "app/helpers/helpers.php"
        ]
    },
Enter fullscreen mode Exit fullscreen mode

Once you add a new path to the files array, you need to dump the autoloader-

composer dump-autoload
Enter fullscreen mode Exit fullscreen mode

Then you can access this "abc()" function from anywhere in this project.

💖 💪 🙅 🚩
armanrahman
Arman Rahman

Posted on June 19, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Create Helper Function in Laravel
laravel Create Helper Function in Laravel

June 19, 2023