How to Create Custom Helper Functions in Laravel 11?
Teams applying “How to Create Custom Helper Functions in Laravel 11?” in a production project can use the related planning guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi, Dev
In this tutorial, I will demonstrate how to craft personalized helper functions within a Laravel 11 application.
While Laravel 11 does furnish helper functions catering to arrays, URLs, routes, paths, and more, occasions arise where bespoke helper functions tailored to specific project requirements become necessary. In such instances, crafting custom helper files equipped with global functions proves invaluable. Here's how you can fashion your own functions seamlessly:
Step for Laravel 11 Create Helper Functions Example
Step 1: Install Laravel 11
Step 2: Create helpers.php File
Step 3: Register File Path In composer.json File
Step 4: Add Route
Run Laravel App
Step 1: Install Laravel 11
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Create helpers.php File
In this step, you need to create `app/Helpers/helpers.php` in your Laravel project and place the following code in that file:
app/Helpers/helpers.php
<?php
use Carbon\Carbon;
/**
* Write code on Method
*
* @return response()
*/
if (! function_exists('convertYmdToMdy')) {
function convertYmdToMdy($date)
{
return Carbon::createFromFormat('Y-m-d', $date)->format('m-d-Y');
}
}
/**
* Write code on Method
*
* @return response()
*/
if (! function_exists('convertMdyToYmd')) {
function convertMdyToYmd($date)
{
return Carbon::createFromFormat('m-d-Y', $date)->format('Y-m-d');
}
}
Step 3: Register File Path In composer.json File
In this step, you have to put the path of the helpers file. So, basically, open the `composer.json` file and put the following code in that file:
composer.json
...
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers/helpers.php"
]
},
...
After registering, we need to run the composer autoload command so that it loads our helper file. Next, run the command below:
composer dump-autoload
Step 4: Add Route
Next, you have to open and update the following routes in the `routes/web.php` file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('call-helper', function(){
$mdY = convertYmdToMdy('2022-02-12');
var_dump("Converted into 'MDY': " . $mdY);
$ymd = convertMdyToYmd('02-12-2022');
var_dump("Converted into 'YMD': " . $ymd);
});
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/call-helper
Output:
string(32) "Converted into 'MDY': 02-12-2022"
string(32) "Converted into 'YMD': 2022-02-12"
Use In Blade File:
You can also use it in a Blade file as shown below:
Date: {{ convertYmdToMdy('2022-02-12') }}
Date: {{ convertMdyToYmd('02-12-2022') }}
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial