Laravel以积极的方式按顺序排列

时间:2017-10-17 16:50:23

标签: php laravel-5 eloquent laravel-eloquent

我有一个运行分组的查询,并按预先加载顺序排序,看起来组合工作正常,但是没有工作。

{
"data": [
    {
        "id": 133,
        "conversation_id": 13,
        "last_sender": null,
        "last_reciever": {
            "id": 55,
            "userName": "buyer",
            "profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
            "firstName": "Matti",
            "lastName": "Rames"
        },
        "msg": "second message 2 to user second",
        "attachment": null,
        "deleted": 0,
        "seen": 0,
        "created_at": "2017-10-17 15:43:14",
        "updated_at": "2017-10-17 15:43:14"
    },
    {
        "id": 132,
        "conversation_id": 11,
        "last_sender": null,
        "last_reciever": {
            "id": 54,
            "userName": "Sadek",
            "profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
            "firstName": "Sadek",
            "lastName": "Hossain"
        },
        "msg": "second message to first user",
        "attachment": null,
        "deleted": 0,
        "seen": 0,
        "created_at": "2017-10-17 15:38:16",
        "updated_at": "2017-10-17 15:38:16"
    },
    {
        "id": 131,
        "conversation_id": 13,
        "last_sender": null,
        "last_reciever": {
            "id": 55,
            "userName": "buyer",
            "profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
            "firstName": "Matti",
            "lastName": "Rames"
        },
        "msg": "second message to second user",
        "attachment": null,
        "deleted": 0,
        "seen": 0,
        "created_at": "2017-10-17 15:37:49",
        "updated_at": "2017-10-17 15:37:49"
    },
    {
        "id": 130,
        "conversation_id": 11,
        "last_sender": null,
        "last_reciever": {
            "id": 54,
            "userName": "Sadek",
            "profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
            "firstName": "Sadek",
            "lastName": "Hossain"
        },
        "msg": "Hi how are you",
        "attachment": null,
        "deleted": 0,
        "seen": 0,
        "created_at": "2017-10-17 15:36:49",
        "updated_at": "2017-10-17 15:36:49"
    }
]

它会返回这样的数据

133

}

但我只想返回前两个最新结果。仅限了132->groupBy('id') 我知道我添加了->groupBy('id')这里没用,并返回所有记录。我添加它只是为了显示我的数据。因此,如果我删除131,则只返回ID 130和{{1}}

请帮忙。谢谢。

1 个答案:

答案 0 :(得分:1)

好的,我解决了它增加一条额外的线:) 我发帖给其他人的帮助。

->orderBy('created_at', 'desc') 
->get()
->unique('conversation_id');