How do I make global helper functions in laravel 5?

Create a new file in your app/Helpers directory name it AnythingHelper.php An example of my helper is :

<?php
function getDomesticCities()
{
$result = \App\Package::where('type', '=', 'domestic')
    ->groupBy('from_city')
    ->get(['from_city']);

return $result;
}

generate a service provider for your helper by following command

php artisan make:provider HelperServiceProvider

in the register function of your newly generated HelperServiceProvider.php add following code

require_once app_path('Helpers/AnythingHelper.php');

now in your config/app.php load this service provider and you are done

'App\Providers\HelperServiceProvider',

An easy and efficient way of creating a global functions file is to autoload it directly from Composer. The autoload section of composer accepts a files array that is automatically loaded.

  1. Create a functions.php file wherever you like. In this example, we are going to create in inside app/Helpers.

  2. Add your functions, but do not add a class or namespace.

    <?php
    
    function global_function_example($str)
    {
       return 'A Global Function with '. $str;
    }
    
  3. In composer.json inside the autoload section add the following line:

    "files": ["app/Helpers/functions.php"]
    

    Example for Laravel 5:

    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": ["app/Helpers/functions.php"] // <-- Add this line
    },
    
  4. Run composer dump-autoload

Done! You may now access global_function_example('hello world') form any part of your application including Blade views.