xxxxxxxxxx
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
xxxxxxxxxx
"autoload": {
"files": [
"app/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
xxxxxxxxxx
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();
xxxxxxxxxx
use Illuminate\Support\Arr;
$array = Arr::add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
$array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
Note the "Help" command sees the first comment block in a function
xxxxxxxxxx
# This is a single line comment
# {
This is a
multiline comment
# }
xxxxxxxxxx
$callback = function ($value) {
return (is_numeric($value)) ? $value * 2 : 0;
};
$result = with(5, $callback);
// 10
$result = with(null, $callback);
// 0
$result = with(5, null);
// 5