loading...

How to create Helpers function in Laravel 8 (Global function)

kingsconsult profile image Kingsconsult Updated on ・2 min read

Good day, today, we are going to create a helper function in laravel 8, this is the same method in creating a helper function in laravel 7. Helper function helps us to create a function that can be called anywhere in our app. That is a global function that can be called both in the views and also in the controller.
Laravel and PHP also provide some basic functions that can be called anywhere, however, there are some times we might need to write our own custom functions that we will need both in the controller and in the views or other parts of our app.
Today, we are going to write a helper function to get the email address of the user that is login into our app. This is a very simple function, just for illustration. But the logic can be applied to any other function, provided the function is correct.

Click on my profile to follow me to get more updates.

Step 1: create a helper file

Go to app/ directory and create a file called helpers.php
app directory

Step 2: Write the helper function

user email function

Step 3: Include it in our composer.json

we need to include the helpers.php file in our composer.json, so that when we autoload, it will load the file.
in the value of "autoload" key, add "files" as a key, with an array as the value, the array will contain "app/helpers.php".
composer.json file

Step 4: Regenerate the list of all classes in the app

composer dump-autoload

composer.json file

That is all, we can now call our function anywhere in our app
In view

 <div>
     @php
        $email = user_email();
     @endphp

        {{ $email }}
 </div>

** In controller**

  $userEmail = user_email();

composer.json file

You can follow me, leave a comment below, suggestion, reaction, or email, WhatsApp or call me, my contacts are on the screenshot. Visit my other posts

Discussion

pic
Editor guide