How To Implement Helpers Methods To Laravel 9 Project ?

Hello you all !

In this content, I will explain to implement helpers methods to Laravel 9 project.

Sometimes, you want to use a function more than a few times in your project and you want to call them globally. Already PHP or Laravel exists similar functions which is created for same reason.

For example :

strtoupper('Aziz Sancar, Nobel Prize');

We called “ststoupper” function to set all letters to uppercase. The function is a global function. You don’t need to implement anything to use that function. Helpers methods help us to write our own functions to use same reason. When you call these functions, you solve your problem easily. So, we call as “Helpers Methods” to describe these functions.

Let’s have own global functions.

1 — Update Your composer.json File

Firstly, we have to update our composer file to implement our own helper file. We have to add following lines to our composer.json.

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

Probably you will need to add only following line to composer file but before that, you need to find “autoload” line. ( add under to “autoload”).

"files": [

2 — Create Helpers File

After updating composer file, we need to create our own helpers file to describe our functions. As you can see in composer.json, we already defined file path for that.


Now, we need to create our file which is mentioned in composer.json.

We have been created our helpers.php file. We will describe our global functions in this file to call anywhere in our application.

3 — Run Composer

After these process, we have to run composer dump autoload command to load helpers file to project. (Note: If your helpers.php file is not in the path which is described in composer file, you will get an error. You must be sure about it first.)

composer dump-autoload

If you did everything right, you loaded helpers file successfully to your project.

Let’s see how it works.

Firstly, We add a basic function to our helpers.php file.

use Carbon\Carbon;

if (!function_exists('getCurrentTime')){
function getCurrentTime(){
return Carbon::now()->format('Y-m-d H:i:s');

As you can see above, our function will be returned current time.

After that, we can add a route to see result but as you can see following image, when we write just “getC” our function is displayed immediately because it’s global function.

Route::get('get-current-time', function (){
echo getCurrentTime();

Our route will be looking as above and when we call that route, we will see a response as below.

  • 2022–06–09 20:15:15

That’s all ! You can use this trick to increase your project capability. It’s really useful and easy to reach solution.

Thank you for your time. You can share your feedback with me or if you have any expectation topic about Laravel, just let me know it and I can write basic content for you !

Enver Şanlı

BackEnd Developer & Social Thinker



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store