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

40

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();

Comments

Submit
0 Comments