Flattening data with same performance for recursive $query->with

痴心易碎 提交于 2020-01-16 10:09:10

问题


I am using Laravel 5.5.13.

Thanks to awesome help from awesome member of SO I currently get nested (and repeated) data by doing this:

public function show(Entity $entity)
{
    return $entity->with([
        'comments' => function($query) {
            $query->with([
                'displayname',
                'helpfuls' => function($query) {
                    $query->with('displayname');
                }
            ]);
        },
        'thumbs' => function($query) {
            $query->with('displayname');
        }
    ])->firstOrFail();
}

This gives me example data like this: https://gist.githubusercontent.com/blagoh/ee5e70dfe35aa5c68b2d445c63887aaa/raw/a0612fb770a27eaacfbb1e87987aa4fd8902a8a3/nested.json

However I want to flatten it to this: https://gist.github.com/blagoh/7076be06c400d04941a0593267e11e81 - look at the version diff we see the changes:

https://gist.github.com/blagoh/7076be06c400d04941a0593267e11e81/revisions#diff-cb567797700e4d4b63b106653162c671R15

We see line 15 is now "helpful_ids": [] and has just array of ids, and then all displaynames and helpfuls were moved to top of array on line 45 and 78.

Is it possible to flatten this data, while keeping same query performance (or better)?

来源:https://stackoverflow.com/questions/47058045/flattening-data-with-same-performance-for-recursive-query-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!