雄辩 - 用资源减少渴望加载关系的数据

发布于 2025-02-02 16:10:54 字数 3357 浏览 2 评论 0原文

我有一个组中用户列表的数据库,只要当前的活动用户能够查看它们,我正在使用急切的加载来加载这些用户。

$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);

现在问题是可以获得相同的问题资源的结果还是实现我的目标的清洁方式?该代码将正常使用,因为该应用程序适用于少数用户和用户组。它将成为较大的用户和用户组的问题。

I have a database of List of Users in a Group and I'm using eager loading to load those Users as long as the current active users have the ability to view them.

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

With that I get a complicated response like shown below

[
    {
        "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"
                        }
                    }
                ]
            }
        ]
    }
]

I would like to simplify it and achieve a much cleaner look while removing duplicate entries

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

With a little bit of foreach trickery I can get something like that:

$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);

Now the question is it possible to get the same result with Resources or is there a much cleaner way of achieving my goal? As much as this code will work as the application is for a small amount of users and User Groups. It will become a problem with larger Users and User Groups.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2025-02-09 16:10:54

您可以尝试仅获取所需的数据。然后使用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 -collection-可用方法

laravel docs-急切的特定列

You could try to get only the data required. And then use Laravel Collection's (magic) methods to format as per your requirement.

$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();

By fetching only the required data via queries will be a performance optimisation. However for larger databases fetching all data at once could be a severe performance hit and may require pagination.

Laravel Docs - Collection - Available Methods

Laravel Docs - Eager loading specific columns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文