Laravel 将所有父类别显示为特定子类别的树

我正在尝试获取子类别的类别树


假设我有一个名为“配件”的子类别


这个子类别有电子>笔记本电脑的父母


所以它是电子产品>笔记本电脑>配件


表 :


-----------------------------------------

| id    | parent_id     | name          |

|----   |-----------    |-------------  |

| 1     | 0             | Electronics   |

| 2     | 1             | Laptops       |

| 3     | 2             | Accessories   |

-----------------------------------------

我可以得到一个子类别的根类别,比如:


function getTopParent($category) {

    if($category->parent_id === null) {

        return $category->name;

    }


    return getTopParent(App\Category::find($category->parent_id));


    // Will return Electronics


}

我也知道如何显示树等类别,请参阅此处


function printCategoryName($categories, $parentName = '') {

    foreach ($categories as $category) {

        $name = $parentName ? implode(' > ', [$parentName, $category->name]) : $category->name;

        echo sprintf('%s%s', $name, PHP_EOL);


        if (count($category->children) > 0) {

            printCategoryName($category->children, $name);

        }

    }

}


printCategoryName($categories);

我需要的是给出一个类别,如配件,并获取此子类别的树和获取类别树:


电子产品>笔记本电脑>配件。


我怎样才能做到这一点?


桃花长相依
浏览 164回答 4
4回答

慕妹3146593

    protected function getCategoriesTree()    {        $categories = Category::where('parent_id',0)->get();        if($categories->count())        {            foreach ($categories as $category)             {                $categories_tree[$category->id] = $this->getChildCategories($category);            }        }        return response()->json(['categories' => $categories_tree]);    }    private function getChildCategories($category)    {        $sub_categories = [];        $childs = Category::where('parent_id', $category->id)->get();        $sub_categories = $category;        $sub_categories['sub_categories'] = [];        if($childs->count())        {            $sub_categories['sub_categories'] = $childs;        }        return $sub_categories;    }

largeQ

这就是我如何让它工作:function getParentsTree($category, $name){    if ($category->parent_id == null)    {        return $name;    }    $parent = Category::find($category->parent_id);    $name = $parent->name . ' > ' . $name;    return getParentsTree($parent, $name);}   $cat = Category::find(1);echo getParentsTree($cat, $cat->name);输出:Electronics > Laptops > Accessories

哈士奇WWW

这是复合设计模式解决的常见问题将对象组合成树结构以表示整个部分的层次结构。复合允许客户端统一处理单个对象和对象组合。递归组合 “目录包含条目,每个条目都可以是一个目录。1对多“有”上“是”层次结构源你可以在这里看到一个使用php实现复合设计模式的例子。

慕丝7291255

我正在使用这种结构您的输入类别模型App\Category.phpclass Categories extends Model{&nbsp; &nbsp; public function parent()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;return $this->hasOne('App\Category', 'parent_id','id');&nbsp; &nbsp; }&nbsp; &nbsp; public function childiren()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;return $this->hasMany('App\Category', 'id','parent_id');&nbsp; &nbsp; }}您的控制器ExampleController.phppublic function example(){&nbsp; &nbsp; $data = Category::all();&nbsp; &nbsp; return view('index', compact('data'));}您的刀片index.blade.php<select>&nbsp; &nbsp; @foreach($data as $categories)&nbsp; &nbsp; &nbsp; &nbsp; <optgroup label="{{ $categories->name }}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($categories->children as $category)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="{{ $category->id }}">{{ $category->name }}</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; </optgroup>&nbsp; &nbsp; @endforeach</select>你也使用jquery选择2
打开App,查看更多内容
随时随地看视频慕课网APP