How to create custom helpers in Laravel?

Search for a command to run...

No comments yet. Be the first to comment.
Last week, I encountered issues with my existing Ubuntu setup. It was slow and lagging, and I couldn't resolve the problem. Then, I remembered that my laptop had two operating systems installed: Windows and Linux on separate partitions. Suspecting a ...
Introduction If you want to learn about how to use console.log as a professional developer then you have to learn computer property names. Let's Start Assume that we have 4 different objects and each one is assigned to a variable. How can we log thos...

Hello artisans, Today I'll be writing about how to use Query Scopes efficiently in Laravel projects. Query Scopes are very useful to encapsulate the logic you want to apply to database queries while using Models. Let's start with an example and under...

Laravel Sanctum is one way to authenticate different models to use your application but there are many other ways. In this article, I'll be explain how to authenticate team/team members with a Laravel Sanctum. Assume that we have following Model; cla...
Introduction: Managing forms in web applications can be a complex task, especially when dealing with validation rules, error handling, and form submissions. The React useForm plugin library offers a powerful and elegant solution to simplify form mana...

Laravel is one of the best frameworks out there FOR ME. I use it every day for my 9-5 job and I really love it.
Whenever I start a new project either for my job or for my side projects, the first thing that I do is to create a new file called helpers.php. I use that file to create the custom helper functions that I will be used for along the project.
For example; I really don't like to find the name of the current route in blade files following a hard way like as below;
{{ Illuminate\Support\Facades\Route::currentRouteName() }}
Instead, I prefer to create a simple function that I can use anywhere in the project to get the current route name in a shorter syntax like as below;
{{ getCurrentRouteName() }} // In the blade files,also can be used in .php files as well.
Since Laravel as a default uses composer to manage the dependencies, we have a powerful tool under our hands and we can tell the composer to autoload any file we want and it allows us to use it in the entire project.
To do so; first of all, we have to create the file anywhere we want, as a default, I always put it under the app folder.
After I am sure that, the file exists, I create my own custom helper functions in the file as shown below;
// function_exists method is initiated to check;
// If there is any function existed with the name of getCurrentRouteName,
// If not, this function will be added otherwise existed one will be used.
// The aim is to protect existing functions to be overwritten
if (!function_exists('getCurrentRouteName')) {
function getCurrentRouteName()
{
return Illuminate\Support\Facades\Route::currentRouteName();
}
}
At this point, I have to tell the composer to discover and autoload this file by adding it to the composer.json file as shown below otherwise my function will not be callable.
If you don't know, composer.json is a file in that you can define the project dependencies and install them using a simple command composer install or update them with composer update.
{
"require": { ... },
"require-dev": { ... },
"autoload": {
"psr-4": { ... },
"files": [
"app/helpers.php" //this is the place I tell composer to autoload file
]
},
...
}
I created a file, add it to the composer.json file, so is it going to work? Unfortunately No 😥
Because I have to run a simple composer command to load all files again in the entire project just for one time.
composer dump-autoload
I am afraid to ask, but is it going to work? Yes! Congratulations, you've made it 😇
So, now I can use getCurrentRouteName anywhere I want as I showed in the example above.
{{ getCurrentRouteName() }} // In the blade files,also can be used in .php files as well.
This is how I create custom helper functions in my Laravel projects.