Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

How to make a custom helper function, available in every controller for Laravel


First create the required function inside the app directory within a .php file as

helpers.php

if (!function_exists('getServices')) {
    public function getServices() {
        return DB::table('services')->get();
    }
}
and include this file in composer.json inside autoload/files array as

composer.json

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App": "app/"
    },
    "files": [
        "app/helpers.php"
    ]
},
Then update the composer, now you can able to directly use the created function inside your whole project as the file is automatically loaded when application get bootstraped

$result = getServices();
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #custom #helper #controller #Laravel
ADD COMMENT
Topic
Name
4+1 =