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

时间:2019-02-20 08:10:42

标签: php laravel sorting laravel-5.7 laravel-collection

我准备好一个api,它返回以下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"
            }
        }
    ]
}

UPDATE : 添加这些行按预期工作。谢谢@JCode

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

1 个答案:

答案 0 :(得分:3)

您正在寻找 sortBy() method

如果可以通过collect()将JSON输出转换为集合,这当然可以工作-在理想情况下,您的聊天消息将是Laravel中的模型。