本文經受權轉自 PHPHub 社區php
使用 Baum 嵌套集合模型來實現 Laravel 模型的無限極分類html
你們一般都是使用遞歸實現無限極分類,都知道遞歸效率很低,下面推薦一個 Laravel 的擴展包 etrepat/baum,快速讓你的數據模型支持無限極樹狀層級結構,而且兼顧效率。laravel
更多 嵌套集合模型(Nested set model)的介紹請見:wiki git
擴展包的 官方文檔 裏有解釋的篇幅,下面這張圖的也是一個簡單的例子:github
接下來說幾個無限樹狀層級模型的例子。數組
一個標籤能夠有無數多子標籤,屬於一個父標籤,有多個同輩標籤。composer
以下面的這顆標籤樹:ide
$tagTree = [ 'name' => 'RootTag', 'children' => [ ['name' => 'L1Child1', 'children' => [ ['name' => 'L2Child1'], ['name' => 'L2Child1'], ['name' => 'L2Child1'], ] ], ['name' => 'L1Child2'], ['name' => 'L1Child3'], ] ];
評論的無限極別嵌套,如網易的 跟帖系統。ui
Laravel 有一個評論擴展包支持無限極別嵌套,請見 Slynova-Org/laravel-commentable。
管理員後臺須要提供「導航欄」自定義功能,樹狀結構導航欄。
etrepat/baum 快速讓你的數據模型支持無限極樹狀層級結構,且兼顧效率。
接下來咱們講如何集成。
composer require "baum/baum:~1.1"
修改 config/app.php
文件,在 providers
數組中添加:
'Baum\Providers\BaumServiceProvider',
此服務提供者註冊了兩個命令:artisan baum
, artisan baum.install
。
安裝到已存在的數據模型上:
php artisan baum:install MODEL
而後執行
php artisan migrate
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(); }); } }
繼承 BaumNode
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'); }
至此集成成功。
集成 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 。