Laravel created_at return object in place of date format in database?

Abhishek Badole picture Abhishek Badole · Dec 26, 2017 · Viewed 16.6k times · Source

I am trying to return created_at datetime from users table in JSON response in Laravel.

In my databaes it show the value as 2016-07-18 00:00:00 but when I try to return in JSON api it converts into

{
    date: "2016-07-18 00:00:00.000000",
    timezone_type: 3,
    timezone: "UTC"
}

How can I fix this problem?

Answer

Marcin Nabiałek picture Marcin Nabiałek · Dec 26, 2017

By default created_at and updated_at are Carbon objects, so you can do just:

$object->created_at->toDateTimeString();

to get again in format Y-m-d H:i:s