In this post we will first learn what is helper function in Laravel and then we will learn how to create custom helper function in Laravel.
Table of Contents
Helper function in Laravel
Overview
Helper functions in Laravel are PHP functions which are pieces of code that are used in many files of the application. Various helper functions are included in the Laravel application.
Some available helper functions
Laravel has various helper functions for array, number, path, URL etc.
There are helper functions like Arr::exists(), Arr::join() etc. for array and Number::format(), Number::currency(), etc. for number.
There are helper functions like app_path(), base_path() etc for path and helper functions like route(), url() etc for URL.
There are also helper functions like auth(), event() etc for miscellaneous purposes.
We can know details about that at https://laravel.com/docs/10.x/helpers.
Now we will know how to create custom helper function in Laravel.
Create custom helper function in Laravel
If we want to create custom helper function in our application we can follow below mentioned steps.
Step 1: Create new directory and new file
First we will create the Helpers directory within app directory and helpers.php file within this Helpers directory.
Step 2: Add helper function in helpers.php
Now we will create a function named custom_date_format.
<?php
function custom_date_format($date,$format){
return \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format($format);
}
?>
Step 3: Add the file path of helpers.php in composer.json file
Composer is a dependency manager for PHP and the composer.json file is a configuration file used by Composer in Laravel. We will include the files property in the autoload property of this file in which we will add the path of helpers.php.
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers/helpers.php"
]
},
Step 4: Run below command in console
The composer dump-autoload look for all the classes and files it needs to include.
composer dump-autoload
Now on every request the helpers.php file will be loaded automatically as Laravel requires Composer’s autoloader that we can find in public/index.php:
require __DIR__.'/../vendor/autoload.php';
Now we can use this helper function anywhere in the application. For example we can use in blade template. For this we will first create a route.
Step 5: Create a new route
Now we will create a new route in routes/web.php.
<?php
use Illuminate\Support\Facades\Route;
Route::get('/custom', function () {
return view('custom');
});
Step 6: Create a view file
For the above route we will create a new a view file named custom.blade.php in resources/views/custom.blade.php.
New Date Format: {{ custom_date_format('2024-02-05','m/d/Y') }}
Step 7: Browser output
Now, if we browse http://<SITEURL>/custom, then we will get the following output:
New Date Format: 02/05/2024
From the above discussion we learned how to create custom helper functions in Laravel. I Hope this post will be useful for you.