Framework: Date Serialization doesn't work

Created on 5 Mar 2020  路  4Comments  路  Source: laravel/framework

  • Laravel Version: 7.0.4
  • PHP Version: 7.4.3
  • Database Driver & Version: MySQL 8.0.16

Description:

Date serialization doesn't work.
https://laravel.com/docs/7.x/eloquent-serialization#date-serialization

Steps To Reproduce:

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)

needs more info

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 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:

All 4 comments

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:

Was this page helpful?
0 / 5 - 0 ratings