1

这个工作正常:

$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 原始 var 转储结果是 100% 相同的。

为什么 diffForHumans() 不能在 db raw 上工作?

4

1 回答 1

0

如果您尝试使用 eloquent 对 updated_at 进行 var_dump

var_dump($eloquent_news->updated_at);

{
        "date": "2015-03-31 06:01:03.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    }

这是由 eloquent 渲染的 updated_at 的实际结果,我不知道如何在 DB::raw 中实现。但至少你现在知道发生了什么。

底线:

雄辩的 updated_at 和 DB::raw 不是 100% 相同的。

于 2015-04-01T02:11:12.620 回答