这个很好用:
$news = NewsFeed::all();
返回是:
{
"new": [
{
"id": "NF_gi042G0493C389rT1z09",
"user_id": "CUS_G530t786S1GVwlcJ3Nw1",
"newsfeedable_id": "STA_7F5eJ6587n2523750cZr",
"newsfeedable_type": "Status",
"created_at": "2015-03-31 06:01:03",
"updated_at": "2015-03-31 06:01:03"
},
{
"id": "NF_0k625I8vp9sG22637a80",
"user_id": "CUS_G530t786S1GVwlcJ3Nw1",
"newsfeedable_id": "CNT_dF22v8dorG2k143N1288",
"newsfeedable_type": "Contact",
"created_at": "2015-03-30 10:04:34",
"updated_at": "2015-03-30 10:04:34"
}
]
}
==============
但是这个:
$news = DB::select( DB::raw(" SELECT * FROM `news_feed` ") );
注意:mysql查询很好,工作正常
我的代码:
$x = $news->updated_at->diffForHumans();
echo $x;
错误:
在非对象
上调用成员函数diffForHumans()
我这里做的var_dump是db raw:
的结果{
"new": [
{
"id": "NF_gi042G0493C389rT1z09",
"user_id": "CUS_G530t786S1GVwlcJ3Nw1",
"newsfeedable_id": "STA_7F5eJ6587n2523750cZr",
"newsfeedable_type": "Status",
"created_at": "2015-03-31 06:01:03",
"updated_at": "2015-03-31 06:01:03"
},
{
"id": "NF_0k625I8vp9sG22637a80",
"user_id": "CUS_G530t786S1GVwlcJ3Nw1",
"newsfeedable_id": "CNT_dF22v8dorG2k143N1288",
"newsfeedable_type": "Contact",
"created_at": "2015-03-30 10:04:34",
"updated_at": "2015-03-30 10:04:34"
}
]
}
Eloquent和DB raw var dump结果是100%相同。
为什么diffForHumans()不能在db raw上工作?
答案 0 :(得分:0)
如果你尝试使用eloquent
var_dump updated_at的var_dump($ eloquent_news->的updated_at);
{
"date": "2015-03-31 06:01:03.000000",
"timezone_type": 3,
"timezone": "UTC"
}
这是由讽刺作者更新的update_at的实际结果,我不知道如何在DB :: raw中发生这种情况。但至少你现在知道发生了什么。
<强>底线:强>
Eloquent updated_at和DB :: raw不是100%相同。