Welcome To ITSolutionsGuides,
In this it solutions guides tutorial lets see How To Create Custom Helper Functions In Laravel 11. Laravel allows the artisan to create the custom helper functions of thier own. Sometimes we need to use the same functions or any of the formating options throughout the application in this cases we can create a custom helper fucntions and that helper functions will be used throughout the application like controller, blade files etc. In this example we will create a custom helper functions to format the date or timestamp.
Lets create the new file helpers.php and then lets create the function to format the date ,
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');
}
}
Lets update the path in the composer.json file,
composer.json
...
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers/helpers.php"
]
},
...
Lets run the below command to let the changes we made in the composer.json to take effect,
composer dump-autoload
Lets see how to use the helper functions in the blade file,
<p>Date: {{ convertYmdToMdy('2022-02-12') }}</p>
<p>Date: {{ convertMdyToYmd('02-12-2022') }}</p>
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides