Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use carbon in Laravel 5.2 application-wide

How to use Carbon in Laravel 5.2 without use Carbon\Carbon; added in every View and Controller..?

like image 821
jahsen Avatar asked Mar 14 '16 23:03

jahsen


2 Answers

Add the following line to the aliases array in the config/app.php:

'Carbon' => 'Carbon\Carbon'

And you need to add use Carbon; every class where you want to use it.

like image 81
zsolt.k Avatar answered Oct 20 '22 22:10

zsolt.k


You can declare some fields in your models using the following structure:

protected $dates = ['created_at', 'updated_at', 'disabled_at','mydate'];

All of those fields will automatically be Carbon instances and you will be able to use them in your views like:

{{ $article->mydate->diffForHumans() }}

This is the answer I provided some time ago here.

And here is the documentation from Laravel for that

like image 26
Hammerbot Avatar answered Oct 20 '22 21:10

Hammerbot