How to create helper functions and use them in laravel 8

October 27, 2021
Laravel
How to create helper functions and use them in laravel 8

In this tutorial, you will learn how to create the helper functions and use them in laravel 8. This article is divided into two parts. First, you will learn how to create the helper functions and then I will cover how to use them.

Sometimes we need to create global functions so that we can use them around the website for example currency conversion, random numbers, etc.

If sounds like the same problem to you then this is the complete guide on how to create the helper function and use them in laravel.

In this tutorial, we will create two functions in the helper file to give us a random number from the range and to capitalize the first letter of the string.

Let’s get started!

How to create helper functions

Step 1: Install Laravel

First of all, you need to install the laravel. I am installing the laravel in helper_tutorial folder.

composer create-project --prefer-dist laravel/laravel helper_tutorial

Step 2: Create helper file in APP folder

The second step is to create the helper file itself. In the app folder of the project create a new file with the name of helpers.php. You can name it anything you want, but in my case, I am naming it helpers.

Step 3: Add functions to the helpers.php

Since we want to create the random number we will add the random_number_generator function and to make the first letter uppercase we will create first_up function into the helpers.php

<?php
  
  function random_number_generator(){
 
    return rand(100, 999);
  }

  function first_up($string){
 
    return ucfirst($string);
  }


Step 4: Add a helper file name composer.json File

Now we need to add app/helpers.php in the composer.json file. In case you don’t know, the composer.json the file is located in the main directory of the project.

Open up the composer.json, scroll down to the autoload object, create the files array if you don’t have in autoload object, and add the helper file name app/helpers.php in that array.

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
       "files": [
 
        "app/helpers.php"
 
    ]
},

Step 5: Run Command for autoloading

Now we need to run the following command in the terminal.

composer dump-autoload

After running the above command, we can use the helper functions.

Just to let you know, this autoloading command was required only one time only when we create a new helper file itself. This means if you add new functions to the same file, you can use them without the need to run the autoload command again.

How to use helper functions

Hope you learned how to create the helper functions. Now, let’s jump the how to use the helper functions in controller and views.

How to use the helper function in the controller?

To use the helper functions we created in the controller, we just have to call them with the function names.

public function index()
{   
    $string = first_up('text');

    $random_number = random_number_generator();
    ....
}

How to use the helper function in the view?

To use the helper functions we created in the view, same as the controller, we just have to call them with the function names.

<h2>{{first_up('text')}}</h2>
<h2>{{random_number_generator()}}</h2>

Conclusion:

Hope you learned today how to create helper functions and how to use helper functions in the controller and view.

Let me know how was the tutorial in the comments below.

See you in the next one!

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon