Using Baum nested set model to implement infinite pole classification of Laravel model

Source: Internet
Author: User

This article is licensed to be transferred from the Phphub community

Using Baum nested set model to implement infinite pole classification of Laravel model

Description

We usually use recursion to achieve infinite pole classification, all know that recursive efficiency is very low, the following is recommended a Laravel extension package Etrepat/baum, quickly let your data model support infinite polar tree hierarchy, and the efficiency.

For more information on nested collection models (Nested set model), see: wiki

There is an explanation in the official documentation for the expansion package, and here is a simple example:

File

Use case description

Let's talk about a few examples of infinite tree-like hierarchy models.

Labeling System

Reference: Laravel taggable add tag to your model A tag can have countless multiple child tags, belong to a parent tag, and have multiple sibling tags.

As in this tag tree below:

$tagTree = [    ' name ' = ' Roottag ',    ' children ' = = [        ' name ' = ' l1child1 ',            ' children ' = [                [' name ' = ' l2child1 '],                [' name ' = = ' l2child1 '],                [' name ' = ' = ' l2child1 '],]        ],        [' name ' = = ' l1child2 '],        [' Name ' = > ' l1child3 '],    ];

Comment System

Comments on the unlimited polar nested, such as NetEase's thread system.

File

Laravel has a comment extension packages support unlimited polar nesting, see slynova-org/laravel-commentable.

The Navigation bar data model

Admin background need to provide "navigation bar" customization function, tree structure navigation bar.

File

Integrated Baum

Etrepat/baum quickly allows your data model to support an infinite tree-like hierarchy with efficiency in the balance.

Next we'll talk about how to integrate.

1. Composer Installation

Composer require "baum/baum:~1.1"

2. Increase Provider

Modify the config/app.php file and add it in the providers array:

' Baum\providers\baumserviceprovider ',

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

3. Create migration

Install to a data model that already exists:

PHP Artisan Baum:install MODEL

And then execute

PHP Artisan Migrate

Introduction To fields about migration

    • PARENT_ID: ID of parent node
    • LFT: Index value on the left
    • RGT: Right index value
    • Depth: Level depth

Here's an example:

Class Category extends Migration {public  function up () {    schema::create (' categories ', function (Blueprint $ Table) {      $table->increments (' id ');         These four lines of code      $table->integer (' parent_id ')->nullable ();      $table->integer (' LfT ')->nullable ();      $table->integer (' RGT ')->nullable ();      $table->integer (' depth ')->nullable ();      $table->string (' name ', 255);      $table->timestamps ();}    );}  }

4. Configure the data model

Inherit Baum\node

Class Category extends Baum\node {}

These properties can be overridden after inheritance:

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

This integration is successful.

Use

Reference: https://phphub.org/topics/2123

The integrated Etrepat/baum allows the label to have a dependency.

$root = tag::create ([' Name ' = ' root ');//Create sub-label $child1 = $root->children ()->create ([' name ' = ' Child1 ']); Child = Tag::create ([' name ' = ' Child2 ']); $child->makechildof ($root);//Bulk build tree $tagtree = [    ' name ' = = ' Roottag ',    ' children ' = = ['        name ' = ' = ' l1child1 ',            ' children ' and ' = ' [                ' name ' = ' l2child1 '],                [' name ' = ' l2child1 '],                [' name ' = = ' l2child1 ']            ,        [' name ' = ' l1child2 '],        [' name ' = ' l1child3 '],    ]; Tag::buildtree ($tagTree);

For more related operations please see: Etrepat/baum.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.