今天手动获取created_at进行操作时意外的发现获取不到,获取到的结果为"2019-05-15T14:09:01.000000Z"很奇怪,手动搜索发现如下解决方式!
问题代码
public function data()
{
$goods=Good::with('category')->get();
$data = [];
foreach ($goods as $k => $item) {
$temp['id'] = $item->id;
$temp['name'] = $item->name;
$temp['created_at'] = $item->created_at;
$temp['photo'] = $item->photo;
$temp['sort'] = $item->sort;
$temp['category'] = $item->category->name;
$data[] = $temp;
}
return $data;
}
显示结果为
image.png
解决方式在模型添加如下代码
protected $casts = array('created_at' => 'created_at','updated_at'=>'updated_at');
image.png
成功解决
网友评论