Home >Backend Development >PHP Tutorial >laravel implements infinite classification
Explanation
Everyone usually uses recursion to implement infinite classification. We all know that recursion is very inefficient. Here is a Laravel expansion package recommended etrepat/baum, quickly make your data model support infinite tree hierarchical structure while taking into account efficiency.
Using the Baum nested set model to implement the infinite classification of the Laravel model
The official documentation of the expansion package has explanation space, and the picture below is also one Simple example:
# Next, let’s talk about a few examples of infinite tree hierarchical models.
#Reference: Laravel Taggable Adds tagging function to your model. A tag can have countless child tags, belong to one parent tag, and have multiple peer tags.
For example, the tag tree below:
$tagTree = [ 'name' => 'RootTag', 'children' => [ ['name' => 'L1Child1', 'children' => [ ['name' => 'L2Child1'], ['name' => 'L2Child1'], ['name' => 'L2Child1'], ] ], ['name' => 'L1Child2'], ['name' => 'L1Child3'], ]];
#Infinitely nested comments, such as NetEase's comment system.
Laravel has a comment extension that supports unlimited nesting, see Slynova-Org/laravel-commentable.
#The administrator backend needs to provide the customization function of "Navigation bar", a tree-structured navigation bar.
#etrepat/baum Quickly make your data model support the infinite tree hierarchical structure while taking into account efficiency.
Next let’s talk about how to integrate.
composer require "baum/baum:~1.1"
#Modify the config/app.php
file in providers
Add to the array:
'Baum\Providers\BaumServiceProvider',
This service provider has registered two commands: artisan baum
, artisan baum.install
.
#Install it on the existing data model:
php artisan baum:install MODEL
Then execute
php artisan migrate
#parent_id: the id of the parent node
lft: the left index value
rgt: the right index value
depth: Hierarchical depth
The following is an example:
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(); }); }}
#Inherit Baum\Node
class Category extends Baum\Node {}
After inheritance, these properties can be overridden:
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');}
The integration is successful.
$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);