Custom Helpers Laravel

custom helpers laravel

Laravel Helpers are very handy in Laravel application development probably you have used some of them throughout the development of your application. For instance, to get the current route with name you can use route('route name') or for application’s base URL we use url() and so on.

These helpers are really helpful but there might be a situation where you want to create your own helper function with custom logic in it and reuse them wherever you want.

In this post, I will walk you through to create your own global helper functions. So let’s start.

Custom Helper Functions using Service Providers

We can add our own custom PHP file into Laravel using a service provider. Run below command to create your own service provider:

Above command will generate below service provider in app/Providers folder.

Now register your HelperServicerProvider in the providers array in config/app.php file like below:

Now we will create a new function inside of HelperServicerProvider called loadHelpers() like below:

In loadHelpers() function we are running a loop on all files inside app/Helpers folder and including into our app.

Now call this function inside register() method of HelperServicerProvider like below:

Your final class will look like below:

Creating Helper File

Now, create a folder inside app folder called Helpers along with a file called functions.php.

Inside functions.php file, we will create a function called app_name() which will return application name.

Now, app_name() is available within your application and you can use it wherever you want by simply calling it like below.


Laravel Helpers provide the ability to create custom functions to execute your custom logic. One practical example could be, if you want to render a menu navigation bar you can create HTML markup within a function and simply call that function in the view.