【问题标题】:Carbon diffforhuman not working on DB rawCarbon diffforhuman 不适用于 DB raw
【发布时间】:2015-04-01 01:38:20
【问题描述】:

这个很好用:

$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 上工作?

【问题讨论】:

标签: php laravel-4 eloquent query-builder php-carbon


【解决方案1】:

如果你尝试使用 eloquent var_dump the updated_at

var_dump($eloquent_news->updated_at);

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

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

底线:

Eloquent updated_at 和 DB::raw 并非 100% 相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2014-11-16
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多