递归获取树的所有父母和孩子

2024-01-26

我有一个 MySQL 表

+--------------------+--------------------+--------------------+
|        Id          |      parent_id     |        title       |
+--------------------+--------------------+--------------------+
|        1           |         0          | Student Management |
|--------------------|--------------------|--------------------|
|        2           |         0          |  Staff Management  |
|--------------------|--------------------|--------------------|
|        3           |         1          |     Add Student    |
|--------------------|--------------------|--------------------|
|        4           |         1          |    View Students   |
|--------------------|--------------------|--------------------|
|        5           |         2          |      Add Staff     |
|--------------------|--------------------|--------------------|
|        6           |         2          |      View Staff    |
|--------------------|--------------------|--------------------|
|        7           |         4          |       Delete       |
|--------------------|--------------------|--------------------|
|        8           |         5          |        Copy        |
+--------------------+--------------------+--------------------+

我想以递归的方式达到我的观点。

所需输出

+-------------------------------+------------------------------+
|      Student Mangement        |         Staff Management     |
|        Add Student            |            Add Staff         |
|       View Student            |              Copy            |
|          Delete               |            View Staff        |
+-------------------------------+------------------------------+

我想得到上面的 MySQL 表作为我上面定义的结构

我的获取方法如下

public function get()
{
    $categories = Categories::where('parent_id', '=', 0)->get();
    $permission = Categories::pluck('title','id')->all();
    return view('create-role')->with(compact('categories'));
}

通过上述方法,我让父母认为

@foreach($categories as $category)
   <li>
     {{ $category->title }}
   </li>
 @endforeach

输出为

学生管理

员工管理

请帮助我如何递归地获得上述结构。


首先定义模型中的关系

public function children() {
    return $this->hasMany(Category::class, 'parent_id', 'id');
}

public function parent() {
    return $this->belongsTo(Category::class, 'parent_id', 'id');
}

那么在你看来,我不知道你有多少个子级别。但有2种方法:

1-最简单的方法
如果你知道,你永远不会超过 3 个级别,只需在你的视图中嵌套 3 个 foreach

首先你急切地询问

$categories = Category::with('children')->get(); //save you some queries 

@foreach($categories as $category)
    @if( $category->children )
        @foreach($category->children as $level2)
            @if($level2->children)
               @foreach($level2->children as $level3)
                   @if($level3->children)
                       //another foreach
                   @endif
                   {{ $level3->title }}
               @foreach
            @endif
            {{ $level2->title }}
        @endforeach
    @endif

    {{ $category->title }}
@endforeach

2-实际的递归方式。
这是实验性的,未经测试
定义递归关系

public function recursiveChildren() {
    return $this->children()->with('recursiveChildren');
    //It seems this is recursive
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

递归获取树的所有父母和孩子 的相关文章

随机推荐