Laravel Custom Helper

version : Laravel 10
Post Time : 12-04-2024

Custom Helper

install laravel

          - composer create-project --prefer-dist laravel/laravel:^10.0 laravel_demo

Create Helpers folder then Create custom_helper.php file inside  :
app/Helpers/custom_hleper.php

then create helper functions in custom_helper.php file
 

                                      
                                        

if (!function_exists('getCategoryList')) {
   function getCategoryList()
   {
       return DB::table('category')->select('*')->where('status','1')->get()->toArray();
   }
}

then Load this helper file
Open the composer.json file from project folder

                                      
                                        

 "autoload": {
       "psr-4": {
         ...
       },
       "files": [
           "app/Helpers/custom_helper.php"
       ]
   },

then run below command in command prompt  :

                                      
                                        

composer dump-autoload

You can now use your custom helper functions from anywhere in your Laravel application. For example:

                                      
                                        

$categoryList = getCategoryList();