How to use Carbon in Laravel 5.2 without use Carbon\Carbon; added in every View and Controller..?
How to use Carbon in Laravel 5.2 without use Carbon\Carbon; added in every View and Controller..?
Here's what I have on my helpers.php
function myCarbon($date)
{
return $date != '' ? \Carbon\Carbon::parse($date) : '-';
}
Then on any controllers and views:
myCarbon($model->field)->format('F d, Y');
And since I usually have mm/dd/yyyy and mm/dd/yyyy H:i on my blades, I have these on my helper file:
function mydateFormat($date)
{
return $date != '' ? myCarbon($date)->format('m/d/Y') : '-';
}
function mytimeFormat($date)
{
return $date != '' ? myCarbon($date)->format('H:i') : '-';
}
function mydateTime($date)
{
return $date != '' ? myCarbon($date)->format('m/d/Y H:i') : '-';
}
Now you can use this application-wide on any controllers and views.
(note: function names are sample only, not what actually I am using, change based on your need)