Laravel n级别

时间:2016-04-02 19:32:13

标签: php mysql eloquent laravel-5.1

我有
创建了两个表:
1.类别

Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');            
        $table->timestamps();
});

2。 category_closure

Schema::create('category_closure', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('ancestor', false, true);
        $table->integer('descendant', false, true);
        $table->integer('depth', false, true);

        $table->foreign('ancestor')->references('id')->on('categories')->onDelete('cascade');
        $table->foreign('descendant')->references('id')->on('categories')->onDelete('cascade');
        $table->timestamps();
    });

在类别表格中,我在category_closure

中有我的类别数据和关系
categories                  category_closure
+----+-----------------+   +----+----------+------------+-------+  
| id | title           |   | id | ancestor | descendant | depth |  
+----+-----------------+   +----+----------+------------+-------+  
| 1  | Real Estate     |   | 1  |     1    |      1     |   0   |  
| 2  | Vehicles        |   | 2  |     2    |      2     |   0   |  
| 3  | Clothing        |   | 3  |     3    |      3     |   0   |  
| 4  | Apartments      |   | 4  |     1    |      4     |   1   |  
| 5  | Used Cars       |   | 5  |     2    |      5     |   1   |  
| 6  | Women Clothing  |   | 6  |     3    |      6     |   1   |  
| 7  | Women T-shirt   |   | 7  |     6    |      7     |   2   |
| 8  | Men's Clothing  |   | 8  |     3    |      8     |   1   |    

现在举例类别服装
3 - 3返回服装 - 衣服 3 - 6返回服装 - 女性的衣服 3 - 8返回服装 - 男士服装 等

问题

我无法理解如何在我的模型中定义Eloquent关系以获得具有后代的类别。

类别模型

class Category extends Model
{
    protected $table = 'categories';    
    protected $hidden = ['id'];
    protected $fillable = ['title'];

    public function nodes()
    {
        return $this->belongsTo(CategoryClosure::class, 'ancestor');
    }
    public function leafs()
    {
        return $this->belongsTo(CategoryClosure::class, 'descendant');
    }    
}

和CategoryClosure

class CategoryClosure extends Model
{
    protected $table = 'category_closure';
    protected $fillable = ['ancestor', 'descendant', 'depth'];

    public function categories()
    {
        return $this->hasMany(Category::class, 'ancestor');
    }
    public function subcategories()
    {
        return $this->hasMany(Category::class, 'descendant');
    }
}

如何从这样的闭包表中获取树?

更新 到目前为止我在mysql级别实现了什么

SELECT * FROM categories JOIN trees ON (categories.id = trees.descendant) WHERE trees.ancestor = 1;

如果选择了类别1,这会给我这个输出

    +----+-------------+-------------+----------+------------+------------+---------------------+---------------------+----+----------+------------+-------+---------------------+---------------------+
    | id | title       | slug        | position | real_depth | deleted_at | created_at          | updated_at          | id | ancestor | descendant | depth | created_at          | updated_at          |
    +----+-------------+-------------+----------+------------+------------+---------------------+---------------------+----+----------+------------+-------+---------------------+---------------------+
    |  1 | Real Estate | real_estate |        0 |          0 | NULL       | 2016-04-04 15:00:00 | 2016-04-04 15:00:00 |  1 |        1 |          1 |     0 | 2016-04-04 20:00:00 | 2016-04-04 20:00:00 |
    |  4 | Apartments  | apartments  |        0 |          1 | NULL       | 2016-04-04 15:00:00 | 2016-04-04 15:00:00 |  2 |        1 |          4 |     1 | 2016-04-04 20:00:00 | 2016-04-04 20:00:00 |
    |  5 | Houses      | houses      |        1 |          1 | NULL       | 2016-04-04 15:00:00 | 2016-04-04 15:00:00 |  3 |        1 |          5 |     1 | 2016-04-04 20:00:00 | 2016-04-04 20:00:00 |
    +----+-------------+-------------+----------+------------+------------+---------------------+---------------------+----+----------+------------+-------+---------------------+---------------------+

所以我知道它正在运作,现在如何将其转移到视图...

1 个答案:

答案 0 :(得分:0)

我建议你使用这个包:https://packagist.org/packages/franzose/closure-table所以你不需要重新发明轮子。 它提供了两种获取整个树的方法:

$tree = Category::getTree();

$treeByCondition = Category::getTreeWhere('position', '>=', 1);