Hi Dev,
Today, In this tutorial i would like to share you how to create custom helper example in laravel you can easily utilize add helper functions in laravel 8 application.
So, mainly you have to inscribe reiterated code in your laravel project so i cerebrate it's better we engender our helper function use everywhere same code.
Here, I will give you full example of laravel 8 engender custom helper functions so follow my below steps.
Step 1: Create helpers.php FileFirst of all we need to integrate app/helpers.php in your laravel project put below following code.
Path : app/helpers.php
<?php
/**
* Write Your Code..
*
* @return string
*/
function changeDateFormate($date,$date_format){
return \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format($date_format);
}
/**
* Write Your Code..
*
* @return string
*/
function thumbImagePath($image_name)
{
return public_path('images/blogs/'.$image_name);
}
Step 2: Add File Path In composer.json File:
In this step, you have to put path of helpers file,so basically open composer.json file and put following code in that file:
Path : composer.json
...
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/helpers.php"
]
},
...
Step 3: Run Command
In this last step you shoud required following command.
composer dump-autoloadCreate Route
Now in this step we will use this custom helper function changeDateFormate() and thumbImagePath() in our web.php file.
Path : routes/web.php
Route::get('helper', function(){
$imageName = 'codewebtuts.png';
$fullpath = thumbImagePath($imageName);
dd($fullpath);
});
Route::get('helper2', function(){
$newDateFormat = changeDateFormate(date('Y-m-d'),'m/d/Y');
dd($newDateFormat);
});
Output
"/var/www/html/blog/public/images/blogs/codewebtuts.png"
"10/09/2021"
So, you can use this helper function in your laravel blade file.
$imageName = 'codewebtuts.png'; $fullpath = thumbImagePath($imageName); print_r($fullpath);
{{ changeDateFormate(date('Y-m-d'),'m/d/Y') }}
It will help you...

Post a Comment