如何在 laravel 5.7 中根据自定义条件对集合进行排序?

How to sort the collection on custom condition in laravel 5.7?

我已经 api 准备好 returns 以下 json 响应。

回复:

{
    "success": true,
    "conversation": [{
            "id": 37,
            "type": "1",
            "name": "Sonali",
            "created_at": "2019-02-18 13:26:10",
            "updated_at": "2019-02-18 20:32:54",
            "unread_count": 2,
            "chat": {
                "id": 357,
                "conversation_id": "23",
                "type": "text",
                "sender_id": "37",
                "receiver_id": "39",
                "data": "{\"text\":\"hello....\"}",
                "delivered_at": "2019-02-20 13:25:27",
                "seen_at": null,
                "created_at": "2019-02-20 13:25:10",
                "updated_at": "2019-02-20 13:25:27",
                "sender_name": "Sonali"
            }
        },
        {
            "id": 38,
            "type": "1",
            "name": "Raviraj",
            "created_at": "2019-02-18 20:23:55",
            "updated_at": "2019-02-18 20:32:47",
            "unread_count": 0,
            "chat": {
                "id": 354,
                "conversation_id": "22",
                "type": "text",
                "sender_id": "39",
                "receiver_id": "38",
                "data": "{\"text\":\"hey....\"}",
                "delivered_at": null,
                "seen_at": null,
                "created_at": "2019-02-20 13:24:35",
                "updated_at": "2019-02-20 13:24:35",
                "sender_name": "Nitesh Kesarkar"
            }
        },
        {
            "id": 27,
            "type": "1",
            "name": "Rakesh",
            "created_at": "2019-02-01 10:48:19",
            "updated_at": "2019-02-07 11:35:10",
            "unread_count": 1,
            "chat": {
                "id": 358,
                "conversation_id": "21",
                "type": "text",
                "sender_id": "27",
                "receiver_id": "39",
                "data": "{\"text\":\"hello\"}",
                "delivered_at": "2019-02-20 13:25:27",
                "seen_at": null,
                "created_at": "2019-02-20 13:25:24",
                "updated_at": "2019-02-20 13:25:27",
                "sender_name": "Rakesh Patil"
            }
        }
    ]
}

此响应包含用户列表及其关联的当前登录用户的最后一条聊天消息。我想先根据最新消息对这个集合进行排序。我怎样才能做到这一点?

排序应基于 chat.created_at 字段。

预期结果:

{
    "success": true,
    "conversation": [
        {
            "id": 27,
            "type": "1",
            "name": "Rakesh",
            "created_at": "2019-02-01 10:48:19",
            "updated_at": "2019-02-07 11:35:10",
            "unread_count": 1,
            "chat": {
                "id": 358,
                "conversation_id": "21",
                "type": "text",
                "sender_id": "27",
                "receiver_id": "39",
                "data": "{\"text\":\"hello\"}",
                "delivered_at": "2019-02-20 13:25:27",
                "seen_at": null,
                "created_at": "2019-02-20 13:25:24",
                "updated_at": "2019-02-20 13:25:27",
                "sender_name": "Rakesh Patil"
            }
        },
        {
            "id": 37,
            "type": "1",
            "name": "Sonali",
            "created_at": "2019-02-18 13:26:10",
            "updated_at": "2019-02-18 20:32:54",
            "unread_count": 2,
            "chat": {
                "id": 357,
                "conversation_id": "23",
                "type": "text",
                "sender_id": "37",
                "receiver_id": "39",
                "data": "{\"text\":\"hello....\"}",
                "delivered_at": "2019-02-20 13:25:27",
                "seen_at": null,
                "created_at": "2019-02-20 13:25:10",
                "updated_at": "2019-02-20 13:25:27",
                "sender_name": "Sonali"
            }
        },
        {
            "id": 38,
            "type": "1",
            "name": "Raviraj",
            "created_at": "2019-02-18 20:23:55",
            "updated_at": "2019-02-18 20:32:47",
            "unread_count": 0,
            "chat": {
                "id": 354,
                "conversation_id": "22",
                "type": "text",
                "sender_id": "39",
                "receiver_id": "38",
                "data": "{\"text\":\"hey....\"}",
                "delivered_at": null,
                "seen_at": null,
                "created_at": "2019-02-20 13:24:35",
                "updated_at": "2019-02-20 13:24:35",
                "sender_name": "Nitesh Kesarkar"
            }
        }
    ]
}

更新: 添加这些行按预期工作。感谢@JCode

        $sorted = $chats->sortByDesc('chat.created_at');
        $chats = $sorted->values()->all();

您正在寻找 sortBy() method

如果您的 JSON 输出可以通过 collect() 转换为集合,这当然会起作用——在完美的情况下,您的聊天消息将是 Laravel 中的模型。