Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel helper function custom

Create a helpers.php file in your app folder and load it up with composer:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App": "app/"
    },
    "files": [
        "app/helpers.php" // <---- ADD THIS
    ]
},

After adding that to your composer.json file, run the following command:

composer dump-autoload
Comment

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

PREVIOUS NEXT
Code Example
Php :: to create html document you require a 
Php :: virtual fields cakephp 4 
Php :: Laravel Retrieving Session Data 
Php :: php 8.0 linux 
Php :: model coomad laravel 
Php :: laravel collection higher order messages 
Php :: show file in folders phpstorm 
Php :: laravel belongsto nested 
Php :: smarty shorthand if 
Php :: To show inline category 
Php :: check if order id exists wordpress woccommerce 
Php :: php check timeout 
Php :: chunk in laravel 
Php :: elasticsearch php search date range 
Php :: how to add posts to php 
Php :: ballerina 
Php :: Call to a member function move() on null 
Php :: array_unshift php 
Php :: php get array key by value 
Php :: Dropzone Attachment Required is not working 
Php :: cast_assoc 
Php :: php split string in half 
Php :: modifier un formulaire php 
Php :: joomla 404 
Php :: laravel change value to intger 
Php :: evaluate home tilde ~ in php 
Php :: ussd php 
Php :: laravel collection median 
Php :: laravel collection load 
Php :: laravel migrate patth 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =