Create custom helper functions in Laravel 5

Create custom helper functions in Laravel 5

Laravel Helper is a simple PHP global functions that you can use to save your time in writing some repetitive codes. Laravel provides more than hundred helper functions to speed up the development process. Helper function can be called from anywhere inside an application. So, it helps to remove code repetitions. Example of commonly used helper functions.

Example of Helper function

Let’s create a slug where each string is separated by dash (-) from a post title . In PHP, you have to write the following code

Using Laravel helper function, you can simply write the following

Method 1: Creating Laravel custom helper function using Composer

Step 1: Create a new directory inside App directory as Helpers. Inside Helpers directory, create a called helpers.php. Add your helper functions in this file.

Step 2:
Laravel won’t recognize our file so we need to autoload it. Autoload helpers.php file using composer.json. Inside composer.json file, add lines as shown:

creating-custom-helpers-in-laravel-5_6

Step 3:
Now, run the dump-autoload command and you can generateRandomString() function from anywhere in your application.

Using our custom Helper Function

We can call our helper function from anywhere. For example, let’s call it from route file

It will give output like: V4Z8TYQF

Method 2: Creating Laravel custom helper function using Service Provider

Step 1:

Create a directory named Helpers inside App directory. In this directory, you will be adding your helper functions files. Writing separate helper function will be good if you want to break down the large single helper functions file into smaller manageable files. For those that may have a lot of helper functions, multiple file organization is a must. This solution is essential.

Let’s create a helper function that generates random string of 8 characters. For this, create a file named RandomString.php inside App/Helpers directory and add following code inside it.

 

Step 2:

Go to your command line and create HelperServiceProvider.php with the following code.

creating-service-provider-in-laravel

This will create a file named HelperServiceProvider.php inside App/Providers directory which looks like below:

helperserviceprovider

Step 3:

The register method is used to register your dependencies, and we’ve exactly done that. Let’s add some code in register function to load the helpers.

Step 4:

Register your service provider in config/app.php.  Find “providers” array and add following code at the bottom of providers.

add service provider in provider array

Also add Alias as below:

This will out a random string like: 9E6X87LS

Using Models in Helper function

Using Models in Helper function is quite easy. We can use any model by importing it in helper function.  For Example, using User.php model as:

Conclusion
The above process shows how to create custom helper functions and how it helps to remove code repetitions. We can create as many helper functions as we want.