簡體   English   中英

遞歸獲取樹的所有父母和孩子

[英]Get all parents and children of a tree recursively

我有一個 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        |
+--------------------+--------------------+--------------------+

我想在我看來遞歸地超越。

所需 Output

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

我想像上面定義的結構一樣超越 MySQL 表

我的get方法是

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

Output 為

學生管理

員工管理

請幫助我如何遞歸地獲得上述結構。

首先定義模型中的關系

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

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

然后在你看來,我不知道你有多少子級別。 但有兩種方法:

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
}

這是使用php(任何框架)執行此操作的簡單方法。

基本上你需要一個簡單數組中的所有項並運行formatTree,但是你可以改變函數來使用object而不是array。

<?php

function formatTree($tree, $parent)
{
    $tree2 = array();
    foreach ($tree as $item) {
        if ($item['parent_id'] == $parent) {
            $tree2[$item['id']] = $item;
            $tree2[$item['id']]['child'] = formatTree($tree, $item['id']);
        }
    }

    return $tree2;
}


//for demo
$beforeTree = [
    ['id' => 1, 'parent_id' => 0],
    ['id' => 2, 'parent_id' => 1],
    ['id' => 3, 'parent_id' => 2],
    ['id' => 4, 'parent_id' => 0],
    ['id' => 5, 'parent_id' => 4],
    ['id' => 6, 'parent_id' => 4],
];
$afterTree = formatTree($beforeTree, 0);

var_dump($afterTree);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM