Date serialization doesn't work.
https://laravel.com/docs/7.x/eloquent-serialization#date-serialization
1) Add this to any model:
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
2) Try to return it directly from the controller (to return json)
There's not much to go on from here... can you please fill out the steps to reproduce and tell what you'd expect to happen?
Ok I've updated the steps but it's pretty straightforward to do it (just like documentation states)
This works perfectly well for me:
Route::get('/', function () {
return User::first();
});
{"id":1,"name":"Mrs. Cleta Flatley IV","email":"[email protected]","email_verified_at":"2020-03-04 10:12:37","created_at":"2020-03-04 10:12:37","updated_at":"2020-03-04 10:12:37"}
Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to report back and I'll gladly help you out and re-open this issue.
Thanks!
@driesvints If you return the user instance from the controller, then the date is serialized according to the format rules on the overwritten serializeDate function. However, if you return the date property, eg:
Route::get('/', function () {
return User::first()->created_at;
});
then the date will be returned in ISO format, ignoring the formatting :disappointed:
Most helpful comment
@driesvints If you return the user instance from the controller, then the date is serialized according to the format rules on the overwritten
serializeDatefunction. However, if you return the date property, eg:then the date will be returned in ISO format, ignoring the formatting :disappointed: