Date Casting for Laravel 6.x and 7.x
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => 'datetime:Y-m-d',
'updated_at' => 'datetime:Y-m-d',
'deleted_at' => 'datetime:Y-m-d h:i:s'
];
It easy for Laravel 5 in your Model add property protected $dates = ['created_at', 'cached_at']
. See detail here https://laravel.com/docs/5.2/eloquent-mutators#date-mutators
Date Mutators: Laravel 5.x
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
}
You can format date like this $user->created_at->format('M d Y');
or any format that support by PHP.
created_at
doesn't contain an instance of a carbon object. – Helbonacreated_at
topayment_date
? Just use$payment->created_at
in your blade template directly. – Disaffirmcreated_at
is a Laravel-internal column, you have it automatically when you use$table->timestamps()
in your migration file. Plus you should NOT set it by your own. – Disaffirm$table->timestamps();
is later used when there is already data around. Then maybe use a "helper" function to handlenull
values, likefunction formatCarbon (Carbon $carbon = null) : string { ... }
– Disaffirm