DEV Community

Cover image for Create Helper Function in Laravel
Arman Rahman
Arman Rahman

Posted on • Updated on

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.

Top comments (0)