简体   繁体   English

从两个表创建树结构

[英]Create a tree structure from two tables

So, my problem is that I need to build a tree with data from two tables.所以,我的问题是我需要用来自两个表的数据构建一棵树。

I have the following tables:我有以下表格:

Category:

| id | parent_id | name           |
|----|-----------|----------------|
| 1  | null      | Category 1     |
| 2  | 1         | Category 1.1   |
| 3  | 2         | Category 1.1.1 |
| 4  | null      | Category 2     |
| 5  | 4         | Category 2.1   |
| 6  | null      | Category 3     |


Layer:

| id | category_id | name    |
|----|-------------|---------|
| 1  | 2           | Layer 1 |
| 2  | 2           | Layer 2 |
| 3  | 3           | Layer 3 |
| 4  | 4           | Layer 4 |
| 5  | 4           | Layer 5 |
| 6  | 5           | Layer 6 |

My Category model:我的类别 model:

class Category extends Model
{
    public function parent()
    {
        return $this->belongsTo('App\Category', 'parent_id');
    }

    public function childrens()
    {
        return $this->hasMany('App\Category', 'parent_id', 'id');
    }

    public function layers()
    {
        return $this->hasMany('App\Layer', 'category_id', 'id');
    }
}

Layer model:层 model:

class Layer extends Model
{
    public function category()
    {
        return $this->belongsTo('App\Category', 'category_id');
    }
}

I'm using the following function to build the category tree:我正在使用以下 function 来构建类别树:

public function index()
{
    $categories = Category::all();
    $layers = Layer::all();

    return $this->buildTree($categories->toArray(), null);
}


function buildTree($categories, $parent_id)
{
    $categoriesTree = [];
    foreach ($categories as $category) {
        $category['folder'] = true;

         if ($category['parent_id'] == $parent_id) {
            $childrens = $this->buildTree($categories, $category['id']);
            if ($childrens) {
                $category['childrens'] = $childrens;
            }

            $categoriesTree[] = $category;
        }
    }

    return $categoriesTree;
}

The above function works well for categories and the response is:上述 function 适用于类别,响应为:

  • Category 1第一类
    • Category 1.1 1.1 类
      • Category 1.1.1类别 1.1.1
  • Category 2第 2 类
    • Category 2.1类别 2.1
  • Category 3第 3 类

But I want to add layers as child of respective category, like the following:但我想将图层添加为相应类别的子级,如下所示:

  • Category 1第一类
    • Category 1.1 1.1 类
      • Category 1.1.1类别 1.1.1
        • Layer 3第 3 层
      • Layer 1第 1 层
      • Layer 2第 2 层
  • Category 2第 2 类
    • Category 2.1类别 2.1
      • Layer 6第 6 层
    • Layer 4第 4 层
    • Layer 5第 5 层
  • Category 3第 3 类

What is the best way to do this?做这个的最好方式是什么?

I suggest using arelationship in your Category model with the Layer model and eager load it.我建议在您的类别model 中使用与model 的关系并急切加载它。 This way you achieve the same result but with less overhead on your buildTree function because Laravel is doing most of the work:这样,您可以获得相同的结果,但 buildTree function 的开销更少,因为 Laravel 正在完成大部分工作:

Category.php model类别.php model

class Category extends Model
{
    // ...

    public function layers()
    {
        return $this->hasMany(Layer::class);
    }

    // ...
}

In your controller:在您的 controller 中:

public function index()
{
    $categories = Category::with('layers')->get();

    // ...
}

This results in an array like this:这会产生一个像这样的数组:

每个类别的急切加载层

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM