Laravel is shipped with multiple helper functions, many of these are used by the framework itself, you can use those functions in your code, but as your codebase grow you might want to add your own custom helpers. For more details about Laravel default helpers check them here.

Create the helper file

Obviously first we need to create the helper file, now when it come to the path feel free to choose the one you like depending on the structure of your project, I personally prefer to put them in app/Helpers/Custom_Helpers.php folder. Now that we've create our Custom_Helpers.php file, let's add a function, maybe one where we pass a username as a parameter and it will return the corresponding user from the Database. We added the function_exists condition just to make sure that we don't override any function with the same name that might be used elsewhere.

use App\User;

if (!function_exists('get_user'))
{
    function get_user($username)
    {
        return User::where('username',$username)->first();
    }
}

Add the helper file in composer.json

Now that we've created our helper file, we need to add it to the autoload section of the coposer.json. so it get called on every request of our application.

"autoload": {
    "classmap": [
        ....
    ],
    "files": [
        "app/Helpers/Custom_Helpers.php"
    ],
    "psr-4": {
        .....
    }
},

For more details on the composer.json schema check out this link. Once you're done run a quick : composer dump-autoload.

Call your helper functions

That's it, now you can call your functions anywhere you want in your application, for example in your controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    public function user_profil()
    {
        $username = Auth::user()->username;
        $user     = get_user($username);
        return view('user.profile',compact('user'));
    }
}

That's all folks, hopefully this tutorial was helpful, and if you have any questions feel free to post them in the comments section, and i will answer them as soon as possible.