Custom Helpers functions in Laravel 6Nov 22, 2019
Laravel ships with lots of inbuilt helper functions like old(), now() etc, however while development you might find a scenario where you would need custom helper functions and it makes lot of sense to use them. So, in the article I'm going to walk you through about how to create your custom helper function in Laravel 6.
To start with, lets create a helper.php file under the app/Helper/helper.php folder
Now, lets create our first helper function. For example, you can create a helper function for carbon like carbon() instead of importing Carbon\Carbon everytime you use it, or you can create a simple helper function to get hold on admin user session instead of accessing it like auth()->guard('admin')->user() etc.
After writing your helper function, now we have to let the composer know that we have added a new file to our codebase and it should autoload the functions whenever we try to access it. So let's add the file path to composer.json file.
Under the autoload key pair value, add the file path under the files
key as mentioned above. And then, finally do
Thats it! Now the function you specified under the app/Helper/helpers.php file will be readily available for you to access it across your codebase