Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

recursion - Flat Laravel collection to tree

I have a Laravel collection of pages - each page has a "parent_id" property. It resembles this.

"pages": [
    {

        "id": 1,
        "title": "Page 1 Level 1",
        "parent_id": 0
    },
    {
        "id": 2,
        "title": "Page 2 Level 2",
        "parent_id": 1
    },
    {
        "id": 3,
        "title": "Page 3 Level 3",
        "parent_id": 2
    },
    {
        "id": 4,
        "title": "Page 4 Level 1",
        "parent_id": 0
    },
    {
        "id": 5,
        "title": "Page 5 Level 2",
        "parent_id": 4
    },
    {
        "id": 6,
        "title": "Page 6 Level 3",
        "parent_id": 5
    },
    {
        "id": 7,
        "title": "Page 7 Level 1",
        "parent_id": 0
    },
    {
        "id": 8,
        "title": "Page 8 Level 2",
        "parent_id": 7
    }
]

What I am trying to do is format the output so they are nested with the correct hierarchy. So for example:

"pages": [
    {
        "id": 1,
        "title": "Page 1 Level 1",           
        "parent_id": 0,
        "children": [
            {
                "id": 2,
                "title": "Page 2 Level 2",           
                "parent_id": 1,
                "children": [
                    {
                        "id": 3,
                        "title": "Page 3 Level 3",           
                        "parent_id": 2,
                        "children": []
                    }
                ]
            },
        ]
    },
    {
        "id": 4,
        "title": "Page 4 Level 1",           
        "parent_id": 0,
        "children": [
            {
                "id": 5,
                "title": "Page 5 Level 2",           
                "parent_id": 4,
                "children": [
                    {
                        "id": 6,
                        "title": "Page 6 Level 3",           
                        "parent_id": 5,
                        "children": []
                    }
                ]
            },
        ]
    },
    {
        "id": 7,
        "title": "Page 7 Level 1",           
        "parent_id": 0,
        "children": [
            {
                "id": 8,
                "title": "Page 8 Level 2",           
                "parent_id": 7,
                "children": []
            },
        ]
    },
]

The hierarchy can be any number of levels deep. I have a nearly working version as shown below, but it does contain a bug. Whilst the various child objects are nesting with their parent, they also remain at the root level. So it looks like duplicates are actually placed in their nested positions.

Can anyone help me finish this off?

PageController

$pages = Page::with('children')->get();

Page

public function directChildren(): HasMany
{
    return $this->hasMany($this, 'parent_id', 'id');
}

public function children(): HasMany
{
    return $this->directChildren()->with('children'));
}
question from:https://stackoverflow.com/questions/65941205/flat-laravel-collection-to-tree

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try something like this to nest these records:

$c = collect($data)->keyBy('id');

$result = $c->map(function ($item) use ($c) {
    if (! ($parent = $c->get($item->parent_id))) {
        return $item;
    }
    $parent->children[] = $item;
})->filter();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...