递归获取树的所有父母和孩子 [英] Get all parents and children of a tree recursively

查看:109
本文介绍了递归获取树的所有父母和孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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表

I want to get above MySQL table as structure I defined above

我的get方法是

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

通过上述方法,我在y视图中使父母成为

With above method, i am getting parents in y view as

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

输出为

学生管理

人员管理

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

Please Help how can I get above-mentioned structure recursively.

推荐答案

首先在模型中定义关系

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

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

那么,在您看来,我不知道您有多少个子级别.但是有两种方法:

Then in your view, I don't know how many sub levels you have. But there are 2 ways:

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

1- The easiest way
If you know, you will never go over 3 levels, just nest 3 foreach in your view

首先您要热切查询

$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-实际的递归方式.
这是实验性的,未经测试
定义递归关系

2- The actual recursive way.
This is experimental and not tested
Define a recursive relationship

public function recursiveChildren() {
    return $this->children()->with('recursiveChildren');
    //It seems this is recursive
}

这篇关于递归获取树的所有父母和孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆