简体   繁体   English

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

[英]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一对多的“有一个”在“是一个”层次结构中

Source来源

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.

相关问题 WooCommerce - 按顺序显示特定类别父级的子类别 email - WooCommerce - Display sub-category from specific category parent in order email 根据所选父类别在下拉列表中显示 Wordpress 子类别 - Display Wordpress Sub-category in dropdown based on the selected Parent Category 要在wordpress php中的URL永久链接中显示类别,子类别,子子类别和帖子名称 - To display category,sub-category,sub-sub categories and postname in url permalink in wordpress php 在子类别页面上显示wordpress子类别帖子 - Display wordpress sub-category posts on sub-category page Laravel获取类别和子类别 - Laravel fetch category and sub-category Laravel子类别和父类别,值未显示 - Laravel Sub Category and Parent Categories, values not showing 在Magento主页上单击子类别时,在新的或现有的phtml页面上显示子类别的所有产品 - Display all products of a sub-category on a new or existing phtml page when clicking on a sub-category from Magento home-page Magento:显示子类别列表 - Magento: Display sub-category list 如何在单个屏幕中按类别和子类别显示所有产品? - How to display all products by category and sub-category wise in a single screen? Laravel 从父类别及其所有子类别中获取所有产品 - Laravel get all products from parent category and all it's sub-categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM