是否可以在laravel eloquent

时间:2017-07-26 23:44:05

标签: php eloquent laravel-5.4

我正在尝试将groupBy与Laravel一起使用。这是我的查询

$msgs=Conversation::where('sender', $sender)
    ->orWhere('reciever', $sender)
    ->orderBy('created_at', 'desc')
    ->with('message')
    ->get();

此查询返回消息表中的许多记录,但我只想检索一个。

这是数据,我想将消息对象的结果分组

{
"data": [
    {
        "id": 27,
        "sender": 10,
        "reciever": 4,
        "created_at": "2017-07-26 22:46:04",
        "updated_at": "2017-07-26 22:46:04",
        "message": [
            {
                "id": 5,
                "conversation_id": 27,
                "last_sender": 10,
                "msg": "5",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:46:04",
                "updated_at": "2017-07-26 22:46:04"
            },
            {
                "id": 6,
                "conversation_id": 27,
                "last_sender": 10,
                "msg": "6",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:46:19",
                "updated_at": "2017-07-26 22:46:19"
            },
            {
                "id": 7,
                "conversation_id": 27,
                "last_sender": 10,
                "msg": "7",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:46:35",
                "updated_at": "2017-07-26 22:46:35"
            },
            {
                "id": 8,
                "conversation_id": 27,
                "last_sender": 10,
                "msg": "8",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:46:36",
                "updated_at": "2017-07-26 22:46:36"
            }
        ]
    },
    {
        "id": 26,
        "sender": 10,
        "reciever": 7,
        "created_at": "2017-07-26 22:40:02",
        "updated_at": "2017-07-26 22:40:02",
        "message": [
            {
                "id": 1,
                "conversation_id": 26,
                "last_sender": 10,
                "msg": "1",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:40:02",
                "updated_at": "2017-07-26 22:40:02"
            },
            {
                "id": 2,
                "conversation_id": 26,
                "last_sender": 10,
                "msg": "2",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:45:36",
                "updated_at": "2017-07-26 22:45:36"
            },
            {
                "id": 3,
                "conversation_id": 26,
                "last_sender": 10,
                "msg": "3",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:45:50",
                "updated_at": "2017-07-26 22:45:50"
            },
            {
                "id": 4,
                "conversation_id": 26,
                "last_sender": 10,
                "msg": "4",
                "deleted": null,
                "seen": null,
                "created_at": "2017-07-26 22:45:57",
                "updated_at": "2017-07-26 22:45:57"
            }
        ]
    }
]

}

非常感谢任何帮助。如果事情不清楚,请告诉我。谢谢。

1 个答案:

答案 0 :(得分:1)

代替'with'替换它:

$msgs=Conversation::where('sender', $sender)
    ->orWhere('reciever', $sender)
    ->orderBy('created_at', 'desc')
    ->with(['message'  => function ($query) { return $query->first();}])
    ->get();

这将只返回一个“消息”而不是消息集合。 你可以在回调函数里面应用它是什么(pluck,select,take ... etc)