laravel implements infinite classification

大家讲道理
Release: 2023-03-05 22:24:02
Original
4198 people have browsed it

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:

laravel implements infinite classification

Use case description

# Next, let’s talk about a few examples of infinite tree hierarchical models.

Tag system

#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'],
    ]];
Copy after login

Comment system

#Infinitely nested comments, such as NetEase's comment system.

laravel implements infinite classification

Laravel has a comment extension that supports unlimited nesting, see Slynova-Org/laravel-commentable.

「Navigation bar」data model

#The administrator backend needs to provide the customization function of "Navigation bar", a tree-structured navigation bar.

laravel implements infinite classification

Integrate Baum

#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.

1. Composer installation

#
composer require "baum/baum:~1.1"
Copy after login

2. Add provider

#Modify the config/app.php file in providers Add to the array:

'Baum\Providers\BaumServiceProvider',
Copy after login

This service provider has registered two commands: artisan baum, artisan baum.install.

3. Create migration

#Install it on the existing data model:

php artisan baum:install MODEL
Copy after login

Then execute

php artisan migrate
Copy after login

Field introduction about migration

  • #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();
    });
  }}
Copy after login

4. Configure the data model

#Inherit Baum\Node

class Category extends Baum\Node {}
Copy after login

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');}
Copy after login

The integration is successful.

Use

$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);
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template