How to add Helper file in Laravel?

To create custom helpers.php file in laravel we need to edit composer.json file in laravel.

In autoload object add the following file array:-

"autoload": {
    "files": [
        "app/helpers.php"
    ]
},

After adding this we need to run the following command Make sure composer is installed.

composer dump-autoload

This will create a helpers.php file in your app folder. Which will be accessed by all controllers and view files.

Best way to add a function in your Helper file is to check if it already exist or not

<?php
if (! function_exists('your_custom_helper')) {
function your_custom_helper($string) {
   // your code
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *