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() }}
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
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.
This is amazing still in 2023