Configuration of Custom Helper in Laravel
The helper file needs to autoload after created. Then configuration will be performed in the helper file. So let's understand how to do this work. Create helper.php inside apps. The path will be apps>helper.php.
You can choose any name for this file like CustomHelper.php, Include.php and Autoload.php. I prefered here helper.php.
I have written hello inside helper.php to test it.
Now autoload to this helper. Go to composer.json create an array named "files" = [] in the autoload and give the path of helper file like given below.
},
"autoload": {
"files": [
"app/helper.php"
],
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Now autoload with the help of command. Open new terminal and type:
This command resets all the settings of composer that's why composer is able to include helper file.
Now the hello text will be visible on the all pages
We can create functions in helper and reuse them again and again like:
if(!function_exists('p')){
function p($data){
echo "";
print_r($data);
echo "";
}
}
if(!function_exists('get_formatted_date')){
function get_formatted_date($date, $format){
$formattedDate = date($format, strtotime($date));
return $formattedDate;
}
}
Use of above functions are given below:
public function store(Request $request){
p($request->all());
@foreach ($customers as $customer)
{{$customer->name}}
{{$customer->email}}
@if ($customer->gender == "M")
Male
@elseif($customer->gender == "F")
Female
@else
Other
@endif
{{$customer->state}}
{{$customer->city}}
{{$customer->address}}
{{get_formatted_date($customer->dob, 'd-M-Y')}}
@if ($customer->status == "1")
Active
@else
Inactive
@endif
@endforeach
Comments
Post a Comment