Sign in
Log inSign up
Create custom Laravel Helper Methods

Create custom Laravel Helper Methods

Adi SK's photo
Adi SK
·Apr 21, 2020

Hi All, this is Adi, with another Laravel article. I wanted to take some time to explain what Laravel’s Helper methods are and how your can create your own ones. I was never a big fan of creating my own customer helper methods when I started with Laravel, but with time, I have started using them more often and these days there’s no Laravel project without customer helpers.

Let’s get started.

What are Laravel Helpers

Laravel Helper methods are "plain old" PHP functions, that you can call from any part of your Laravel Application. You may have already used a lot of Laravel's built-in helper methods like route(), url(), view(), dd(), and a list of others. You do not have to require any PHP files or reference any namespace to use them, they are magically available in all parts of your application. I'll explain more about the magic part in just a moment.

Here are a few use cases for creating custom helper methods. To call other utility classes, if there's a generic logic that's used multiple times in your code you might want to extract it out into a helper. I extensively use helpers to make formatting easier in my Blade view, such as automatically adding a currency symbol before a number or to format a number in a certain digit grouping or set dates in your application to use the same format on display.

Now let's see how simple it is to create custom Helper methods.

Creating Custom Helpers

It is as simple as creating a file somewhere in the app directory and inform composer to auto-load it when the application starts, simple is it not. I usually have all my helper functions in a single file called Helpers.php in the app/Utils/ folder, this is where I store all my utility classes but you can create this file anywhere in the app directory you see fit. Then, you need to register this file in your composer.json file that's at the root of your project. For reference you can check out how the Laravel framework registers it's helpers here.

composer config And that's it. Make sure to run this command composer dump-autoload and restart your application. This will make sure that the Helper.php is loaded when Laravel is started. Now let's define your first helper method in the Helper.php file.

create customer helper This say_hi method can now be accessed in any part of your application. It's a simple example but you can make it as complex as needed.

Conclusion

I hope you found this article on Laravel helpful. Laravel Helpers are such a handy feature that saves a lot of time. Hope you see the reasons to create your custom ones.

Useful Resources: laravel.com/docs/7.x/helpers laravel.com/docs/7.x/facades#facades-vs-he…

Thank You That's all for now, this has been Adi.
If you are looking for a Freelance Web Developer you can contact me

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct