按对象字段排序Laravel对象集合

eufgjt7s  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(171)

我需要根据对象字段对一组对象进行排序。我见过很多类似的问题,但没有一个解决方案起作用。以下是我目前的解决方案。

$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降序排序。

q43xntqr

q43xntqr1#

您可以在集合上调用以下内容:

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

这将按照您在那里拥有的任何模型的顺序字段以升序排序。
如果要按降序排序,请执行以下操作:

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

oknwwptz2#

只是为了更新第一个答案

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

$sorted = $flatActivities->sortByDesc('order')->values();

需要将values()函数设置为“values方法返回一个键重置为连续整数的新集合:“

相关问题