雄辩 - 用资源减少渴望加载关系的数据
我有一个组中用户列表的数据库,只要当前的活动用户能够查看它们,我正在使用急切的加载来加载这些用户。
$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试仅获取所需的数据。然后使用Laravel
Collection
的(魔术)方法根据您的要求进行格式化。通过仅通过查询获取所需的数据将是性能优化。但是,对于较大的数据库,一次获取所有数据可能是严重的性能命中,可能需要分页。
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.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