After upgrading to Laravel 5.8 the "created_at" field is returning a string like this
"createdAt": "2019-05-01T16:36:25.000000Z"
But I want to return an object like before in Laravel 5.7 like this:
"createdAt": { "date": "2019-05-01 19:36:25.000000", "timezone_type": 3, "timezone": "Asia/Baghdad" }
I did look around the web found nothing about it and there's nothing about it too in upgrade guide too. I'm returning it from resources just like that a JSON:
public function toArray($request)
{
return [
'id' => $this->id,
'createdAt' => $this->created_at,
];
}
That's how I could solve it:
public function toArray($request)
{
return [
'id' => $this->id,
'createdAt' => $this->created_at,
'date' => Carbon::serializeUsing(function ($createdAt) {
return [
'date' => $createdAt->toDateTimeString(),
'timezone_type' => $createdAt->timezone_type,
'timezone' => $createdAt->tzName,
];
}),
];
}
Now it returns an object like this:
"createdAt": {
"date": "2019-05-01 19:36:25",
"timezone_type": 3,
"timezone": "Asia/Baghdad"
},
"date": null
The breaking change is documented in the official Carbon documentation .
尝试返回日期的碳实例:
Carbon::parse($this->created_at)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.