【问题标题】:How to sort the collection on custom condition in laravel 5.7?如何在 laravel 5.7 中根据自定义条件对集合进行排序?
【发布时间】:2019-02-20 16:34:21
【问题描述】:

我已经准备好返回以下 json 响应的 api。

回复:

{
    "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();

【问题讨论】:

  • 这是什么意思:DB::table('chart')->orderBy('user_registered', 'desc')->get()->toArray() ?
  • @ViperTecPro - $sorted = $chats->sortByDesc('chat.created_at'); $chats = $sorted->values()->all(); 这是我按照答案所做的......
  • 这是否解决了您的问题?

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


【解决方案1】:

您正在寻找 sortBy() method

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

【讨论】:

    猜你喜欢
    • 2017-06-03
    • 2014-06-25
    • 2018-03-05
    • 1970-01-01
    • 2020-01-09
    • 2019-02-21
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多