使用 Baum 嵌套集合模型来实现 Laravel 模型的无限极分类

使用 Baum 嵌套集合模型来实现 Laravel 模型的无限极分类

说明

大家通常都是使用递归实现无限极分类,都知道递归效率很低,下面推荐一个 Laravel 的扩展包 etrepat/baum,快速让你的数据模型支持无限极树状层级结构,并且兼顾效率。

更多 嵌套集合模型(Nested set model)的介绍请见:wiki

扩展包的 官方文档 里有解释的篇幅,下面这张图的也是一个简单的例子:

file

用例说明

接下来讲几个无限树状层级模型的例子。

标签系统

参考:Laravel Taggable 为你的模型添加打标签功能
一个标签可以有无数多子标签,属于一个父标签,有多个同辈标签。

如下面的这颗标签树:

$tagTree = [
    'name' => 'RootTag',
    'children' => [
        ['name' => 'L1Child1',
            'children' => [
                ['name' => 'L2Child1'],
                ['name' => 'L2Child1'],
                ['name' => 'L2Child1'],
            ]
        ],
        ['name' => 'L1Child2'],
        ['name' => 'L1Child3'],
    ]
];

评论系统

评论的无限极别嵌套,如网易的 跟帖系统

file

Laravel 有一个评论扩展包支持无限极别嵌套,请见 Slynova-Org/laravel-commentable

「导航栏」数据模型

管理员后台需要提供「导航栏」自定义功能,树状结构导航栏。

file

集成 Baum

etrepat/baum 快速让你的数据模型支持无限极树状层级结构,且兼顾效率。

接下来我们讲如何集成。

1. composer 安装

composer require "baum/baum:~1.1"

2. 增加 provider

修改 config/app.php 文件,在 providers 数组中添加:

'Baum\Providers\BaumServiceProvider',

此服务提供者注册了两个命令:artisan baum, artisan baum.install

3. 创建 migration

安装到已存在的数据模型上:

php artisan baum:install MODEL

然后执行

php artisan migrate

关于 migration 的字段介绍

  • parent_id: 父节点的 id
  • lft: 左边索引值
  • rgt: 右边索引值
  • depth: 层级深度

下面是个例子:

class Category extends Migration 
{
  public function up() {
    Schema::create('categories', function(Blueprint $table) {
      $table->increments('id');

         // 这四行代码
      $table->integer('parent_id')->nullable();
      $table->integer('lft')->nullable();
      $table->integer('rgt')->nullable();
      $table->integer('depth')->nullable();

      $table->string('name', 255);

      $table->timestamps();
    });
  }
}

4. 配置数据模型

继承 Baum\Node

class Category extends Baum\Node 
{
}

继承后有这些属性可以重写:

class Category extends Baum\Node 
{
  protected $table = 'categories';

  // 'parent_id' column name
  protected $parentColumn = 'parent_id';

  // 'lft' column name
  protected $leftColumn = 'lidx';

  // 'rgt' column name
  protected $rightColumn = 'ridx';

  // 'depth' column name
  protected $depthColumn = 'nesting';

  // guard attributes from mass-assignment
  protected $guarded = array('id', 'parent_id', 'lidx', 'ridx', 'nesting');
}

至此集成成功。

使用

引用:分享:【插件发布】 Laravel Taggable 为你的模型添加打标签功能

集成 etrepat/baum 让标签具备从属关系。


$root = Tag::create(['name' => 'Root']);

// 创建子标签
$child1 = $root->children()->create(['name' => 'Child1']);

$child = Tag::create(['name' => 'Child2']);
$child->makeChildOf($root);

// 批量构建树
$tagTree = [
    'name' => 'RootTag',
    'children' => [
        ['name' => 'L1Child1',
            'children' => [
                ['name' => 'L2Child1'],
                ['name' => 'L2Child1'],
                ['name' => 'L2Child1'],
            ]
        ],
        ['name' => 'L1Child2'],
        ['name' => 'L1Child3'],
    ]
];

Tag::buildTree($tagTree);

更多关联操作请查看:etrepat/baum

摈弃世俗浮躁,追求技术精湛
本帖已被设为精华帖!
Summer
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 14

一直没弄懂这个项目干啥用的

终于知道你的庐山真面目

7年前 评论

有和这个配合的前端组件吗?

7年前 评论

Method Baum\Console\InstallCommand::handle() does not exist

6年前 评论

@你在害怕什么

simplethemes commented 2 days ago
Temporarily changing fire() to handle() in vendor/baum/baum/src/Baum/Console/InstallCommand.php resolved this for me.

看到的一个laravel5.5解决方案

6年前 评论

试用了一下,一篇文章多个评论下插入数据需要更新该文章下所有评论的lef 和rig ?
这样评论多并发高的时候效率问题呢?

6年前 评论

@darkcoffee PHP 一般都不怎么考虑并发,本身的同步阻塞机制就限制了高并发~

6年前 评论

亲们,有没有中文手册求分享一个

5年前 评论

GitHub这个包已经是三年前的东西了,好久没再更新过了,目前不支持laravel 5.5,执行bucm:install命令直接报错

Method Baum\Console\InstallCommand::handle() does not exist

关于这个问题的讨论(https://github.com/etrepat/baum/issues/283
,有人fork了一份在做后续的开发,但是2.0版本正在开发,不建议使用

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!