We have already discussed creating custom helper functions (Global Function) in Laravel in a previous article. So today’s article we discuss a different approach to achieve the same feature, and this function can be called from the blade template as well.
Step 1. Add this code in your composer.json file located in the root directory
"autoload": { "psr-4": { "App\\": "app/" }, "files": [ "app/Http/Helpers/Helper.php" ] },
Step 2. Create a file called Helper.php in app/Http/Helpers folder with a sample function
<?php namespace App\Http\Helpers; class Helper { public static function currentTime() { return time(); } }
Step 3. Add alias method in config/app.php
'Helper'=> App\Http\Helpers\Helper::class,
That’s it you can use your function in the blade template like the below snippet
{{ Helper::currentTime() }}
I don’t know how to thank you man. I almost gave up coding because I couldn’t get to use functions in my blade. You just saved me man. Keep the good work up.
Really helpful.