Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel created_at return object in place of date format in database? [closed]

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?

like image 812
Abhishek Badole Avatar asked Dec 26 '17 10:12

Abhishek Badole


2 Answers

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

like image 193
Marcin Nabiałek Avatar answered Oct 23 '22 12:10

Marcin Nabiałek


Since you want to get JSON response, you can use Eloquent API Resource to transform Carbon object to any format, for example:

return [
    'created_at' => $this->created_at->toDateTimeString(),
    ....
];
like image 22
Alexey Mezenin Avatar answered Oct 23 '22 14:10

Alexey Mezenin