[英]Laravel display all parent categories as tree for a specific sub-category
I'm trying to get categories tree of a sub-category我正在尝试获取子类别的类别树
Let's say i have a sub-category called Accessories假设我有一个名为配件的子类别
this sub-category have parents of Electronics > Laptops这个子类别有电子产品 > 笔记本电脑的父母
So it is Electronics > Laptops > Accessories所以它是电子 > 笔记本电脑 > 配件
Table :表:
-----------------------------------------
| id | parent_id | name |
|---- |----------- |------------- |
| 1 | 0 | Electronics |
| 2 | 1 | Laptops |
| 3 | 2 | Accessories |
-----------------------------------------
I can get the root category of a sub-category like :我可以获得子类别的根类别,例如:
function getTopParent($category) {
if($category->parent_id === null) {
return $category->name;
}
return getTopParent(App\Category::find($category->parent_id));
// Will return Electronics
}
Also i know how to display categories like tree, see here我也知道如何显示树之类的类别, 请参见此处
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);
What i need is give a category like Accessories and get tree and get category tree for this sub-category :我需要的是提供一个像配件这样的类别,并为这个子类别获取树和类别树:
Electronics > Laptops > Accessories .电子产品 > 笔记本电脑 > 配件。
How can i achieve this?我怎样才能做到这一点?
This is how i got it working :这就是我让它工作的方式:
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);
Output : Electronics > Laptops > Accessories
输出:
Electronics > Laptops > Accessories
您应该阅读嵌套集模型
This is a Common problem solved by the composite design pattern这是复合设计模式解决的常见问题
Compose objects into tree structures to represent whole-part hierarchies.
将对象组合成树结构以表示整体部分层次结构。 Composite lets clients treat individual objects and compositions of objects uniformly.
Composite 允许客户端统一处理单个对象和对象的组合。 Recursive composition "Directories contain entries, each of which could be a directory."
递归组合“目录包含条目,每个条目都可以是一个目录。” 1-to-many "has a" up the "is a" hierarchy
一对多的“有一个”在“是一个”层次结构中
You can see an example of composite design pattern implementation with php here您可以在此处查看使用 php 实现复合设计模式的示例
I'm using this structure我正在使用这种结构
your enter category model App\\Category.php
您输入的类别模型
App\\Category.php
class Categories extends Model
{
public function parent()
{
return $this->hasOne('App\Category', 'parent_id','id');
}
public function childiren()
{
return $this->hasMany('App\Category', 'id','parent_id');
}
}
your controller ExampleController.php
你的控制器
ExampleController.php
public function example()
{
$data = Category::all();
return view('index', compact('data'));
}
your blade index.blade.php
你的刀片
index.blade.php
<select>
@foreach($data as $categories)
<optgroup label="{{ $categories->name }}">
@foreach($categories->children as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</optgroup>
@endforeach
</select>
also you use jquery select 2你也使用jquery select 2
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.