I had an issue when developing a Laravel 10.x application where I tried to use format()
on the deadline_at
property.
{{ $project->deadline_at->format('d M Y') }}
It was generating an error "Call to a member function format() on string" as you can see on the attached screenshot.
I have added the following attribute to my Laravel Eloquent Model:
protected $dates = [
'start_at',
'deadline_at',
];
So in order to solve this error in Laravel 10.x is to not use the protected $dates
, instead use protected casts
where we added the property that should be mutated to dates.
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'deadline_at' => 'datetime',
];
If your Laravel application version is below 10.x, such as Laravel 9.x, you can still use the protected $dates
.
protected $dates = [
'start_at',
'deadline_at',
];
Reference: