按对象字段对对象的Laravel集合进行排序

时间:2019-06-05 16:55:01

标签: laravel sorting eloquent spatie

我需要根据对象字段对对象集合进行排序。 我已经看到许多类似的问题,但是没有一个解决方案有效。这就是我到目前为止所拥有的。

        $activities = collect();
        foreach(request()->user()->currentTeam->users as $user) {
        $activities->push(Activity::where('description', 'like', "%$query%")
            ->orWhere('properties->name', 'like', "%$query%")
            ->causedBy($user)->latest()->get()->load('causer'));
        }

       $flatActivities = $activities->collapse();

       $sorted = $flatActivities->sort(function($a, $b)
       {
           dd($a->id, $b->id);
           if ($a->id == $b->id) {
              return (0);
           }
           return (($a->id > $b->id) ? -1 : 1);
       });

除了上面具有的排序功能外,我还尝试过sortBy(id)sortByDesc(),但都没有做任何事情。

现在,由于我正在查询每个用户的活动日志,因此它是基于该特定用户的latest活动进行排序的。理想情况下,我希望能够通过查询执行此操作,但是由于要获取每个用户的数据,因此我看不到这是怎么回事。

如果将集合转换为数组,我可以执行此操作,但是由于其他一些限制,我需要将其保留为对象集合。

编辑

请清楚,这是我的数据结构。

{
"0": {
    "id": 4740,
    "log_name": "default",
    "description": "Login: Advisor User logged in from 127.0.0.1.",
    "subject_id": null,
    "subject_type": "App\\User",
    "causer_id": 4,
    "causer_type": "App\\User",
    "properties": {
        "ip_address": "127.0.0.1",
        "name": "Advisor User",
        "action": "login",
        "ip": "127.0.0.1"
    },
    "created_at": "2019-06-05 14:33:34",
    "updated_at": "2019-06-05 14:33:34",
    "causer": {
        "id": 4,
    }
},
"1": {
    "id": 4737,
    "log_name": "default",
    "description": "Login: Advisor User logged in from 127.0.0.1.",
    "subject_id": null,
    "subject_type": "App\\User",
    "causer_id": 4,
    "causer_type": "App\\User",
    "properties": {
        "ip_address": "127.0.0.1",
        "name": "Advisor User",
        "action": "login",
        "ip": "127.0.0.1"
    },
    "created_at": "2019-06-05 14:27:58",
    "updated_at": "2019-06-05 14:27:58",
    "causer": {
        "id": 4,
    }
},
"2": {
    "id": 4735,
    "log_name": "default",
    "description": "Login: Advisor User logged in from 127.0.0.1.",
    "subject_id": null,
    "subject_type": "App\\User",
    "causer_id": 4,
    "causer_type": "App\\User",
    "properties": {
        "ip_address": "127.0.0.1",
        "name": "Advisor User",
        "action": "login",
        "ip": "127.0.0.1"
    },
    "created_at": "2019-06-04 19:26:49",
    "updated_at": "2019-06-04 19:26:49",
    "causer": {
        "id": 4,
    }
},
"3": {
    "id": 4739,
    "log_name": "default",
    "description": "Login: test test logged in from 127.0.0.1.",
    "subject_id": null,
    "subject_type": "App\\User",
    "causer_id": 7,
    "causer_type": "App\\User",
    "properties": {
        "ip_address": "127.0.0.1",
        "name": "test test",
        "action": "login",
        "ip": "127.0.0.1"
    },
    "created_at": "2019-06-05 14:32:55",
    "updated_at": "2019-06-05 14:32:55",
    "causer": {
        "id": 7,
    }
},
}

我需要按对象ID降序排列。

1 个答案:

答案 0 :(得分:1)

您可以在收藏夹中调用以下内容:

$sorted = $flatActivities->sortBy('order');

这将根据您在那里拥有的任何型号的订单字段以升序对其进行排序。

如果要按降序对其进行排序,请执行以下操作:

$sorted = $flatActivities->sortBy('order', SORT_REGULAR, true);`