Eloquent - 从与资源的 Eager Load 关系中减少数据

Eloquent - Reduce the data from an Eager Loaded Relationsip with Resources

我有一个组中用户列表的数据库,只要当前活动用户能够查看这些用户,我就会使用预加载来加载这些用户。

$grouplist = GroupList::with('groups.listOfUsersInGroup.userInfo')
    ->where('user_id', auth()->user()->id)
    ->get();

然后我得到如下所示的复杂响应

[
    {
        "id": 2,
        "group_id": "1",
        "user_id": "1",
        "groups": [
            {
                "id": 1,
                "name": "Group 1",
                "list_of_users_in_group": [
                    {
                        "id": 1,
                        "group_id": "1",
                        "user_id": "1",
                        "user_info": {
                            "fullname": "User 1",
                            "uid": "001"
                        }
                    },
                    {
                        "id": 9,
                        "group_id": "1",
                        "user_id": "2",
                        "user_info": {
                            "fullname": "User 2",
                            "uid": "002"
                        }
                    },
                    {
                        "id": 10,
                        "group_id": "1",
                        "user_id": "5",
                        "user_info": {
                            "fullname": "User 3",
                            "uid": "003"
                        }
                    }
                ]
            }
        ]
    },
    {
        "id": 13,
        "group_id": "2",
        "user_id": "1",
        "groups": [
            {
                "id": 1,
                "name": "Group 1",
                "list_of_users_in_group": [
                    {
                        "id": 1,
                        "group_id": "2",
                        "user_id": "1",
                        "user_info": {
                            "fullname": "User 1",
                            "uid": "001"
                        }
                    },
                    {
                        "id": 9,
                        "group_id": "2",
                        "user_id": "4",
                        "user_info": {
                            "fullname": "User 4",
                            "uid": "004"
                        }
                    }
                ]
            }
        ]
    }
]

我想简化它并在删除重复条目的同时实现更简洁的外观

[
    {
        "fullname": "User 1",
        "uid": "001"
    },
    {
        "fullname": "User 2",
        "uid": "002"
    },
    {
        "fullname": "User 3",
        "uid": "003"
    },
    {
        "fullname": "User 4",
        "uid": "004"
    }
]

通过一些 foreach 技巧,我可以得到类似的东西:

$users = [];

$count = 0;

foreach ($grouplist as $collectionOfGroupLIst) {
    // $count++;
    foreach ($collectionOfGroupLIst->groups as $listOfGroups) {
        foreach ($listOfGroups->listOfUsersInGroup as $listOfUsers) {
            $users[$count] = $listOfUsers->userInfo;

            $count++;
        }
    }
}

return array_unique($users, SORT_REGULAR);

现在的问题是,是否可以使用 Resources 获得相同的结果,或者是否有更简洁的方法来实现我的目标?此代码将尽可能多地工作,因为该应用程序适用于少量用户和用户组。这将成为更大的用户和用户组的问题。

您可以尝试只获取所需的数据。然后使用 Laravel Collection 的(魔术)方法根据您的要求进行格式化。

$groupList = GroupList::with([
    'groups:id',
    'groups.list_of_users_in_group:id,group_id,user_id',
    'groups.list_of_users_in_group.user_info',
])
->where('user_id', auth()->id())
->select('id', 'group_id')
->get()
->pluck('groups')
->flatten(1)
->pluck('list_of_users_in_group')
->flatten(1)
->pluck('user_info')
->unique();

通过查询仅获取所需数据将是一种性能优化。但是,对于较大的数据库,一次获取所有数据可能会严重影响性能并且可能需要分页。

Laravel Docs - Collection - Available Methods

Laravel Docs - Eager loading specific columns