PHP code example of jiaxincui / closure-table

1. Go to this page and download the library: Download jiaxincui/closure-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jiaxincui / closure-table example snippets




$menu = Menu::find(2);
  
// 将$menu作为根,return bool
$menu->makeRoot();
  
// 创建一个子级节点,return new model
$menu->createChild($attributes);
  
// 创建一个新的节点,该节点为根(如果未指定parent),也可以指定列 parent,它将自动维护树结构.,return new model
$child = Menu::create($attributes);
  
// 将一个已存在的节点添加到子级,$child参数可以是模型实例/集合/id/包含id的数组,return bool
$menu->addChild($child);
$menu->addChild(12);
$menu->addChild('12');
$menu->addChild([3, 4, 5]);
  
// 移动到$parent的下级,它的所有下级节点也将随之移动,$parent参数可以是模型实例/id,return bool
$menu->moveTo($parent);
$menu->moveTo(2); 
$menu->moveTo('2');
  
// 添加一个或多个同级节点,$siblings的所有下级节点也将随之移动,$siblings可以是模型实例/集合/id/包含id的数组,return bool
$menu->addSibling($siblings);
$menu->addSibling(2);
$menu->addSibling('2');
$menu->addSibling([2,3,4]);
  
// 新建一个同级节点,return new model
$menu->createSibling($attributes);
  


$menu = Menu::find(3);
  
// 获取所有后代,return model collection
$menu->getDescendants();
  
// 获取所有后代,包括自己,return model collection
$menu->getDescendantsAndSelf();
 
 // 获取所有祖先,return model collection
$menu->getAncestors();
  
// 获取所有祖先,包括自己,return model collection
$menu->getAncestorsAndSelf();
  
// 获取所有儿女(直接下级),return model collection
$menu->getChildren();
  
// 获取上级节点,return model
$menu->getParent();
  
// 获取根(根节点返回本身),return model
$menu->getRoot();

// 获取所有同级节点, return model collection
$menu->getSiblings();
  
//获取所有同级节点并包括本身,return model collection
$menu->getSiblingsAndSelf();
  
// 获取所有孤立节点(孤立节点指在没有在 closureTable 表里维护的记录)
Menu::getIsolated();

// 使用范围查询孤立节点  
Menu::isolated()->where('id', '>', 5)->get();
  
// 获取所有根
Menu::getRoots();
  
// 一个scope,同getRoots()
Menu::onlyRoot()->get();



$menu = Menu::find(3);
  
// 从当前节点生成树,return tree
$menu->getTree();
  
// 当前节点作为根生成树,以sort字段排序,return tree
$menu->getTree(['sortColumn', 'desc']);
  
// 同上,return tree
$menu->getDescendantsAndSelf()->toTree();
  
// 获取到以所有children为根的multi tree
$menu->getDescendants()->toTree();
  
// 从根节点生成树,return tree
$menu->getRoot()->getTree();

//旁树,不包含自己和下级,return tree
$menu->getBesideTree();


[
    [
        'id' => 3,
        'name' => 'node3',
        'children' => [
            [
                'id' => 4,
                'name' => 'node4'
            ],
            [
                'id' => 5,
                'name' => 'node5'
                'children' => [
                    [
                        'id' => 6,
                        'name' => 'node6'
                    ]
                ]
            ]
        ]
    ]
]

    
    
    $multiTree = Menu::all()->toTree();
  
    var_dump($multiTree);
  
    



$menu = Menu::find(3);
  
// 是否根
$menu->isRoot();
  
// 是否叶子节点
$menu->isLeaf();
 
// 是否孤立节点
$menu->isIsolated();
  
// 是否某的上级
$menu->isAncestorOf($descendant);
  
// 是否某的下级
$menu->isDescendantOf($ancestor);
  
// 是否某的直接下级
$menu->isChildOf($parent);
  
// 是否某的直接上级
$menu->isParentOf($child);
  
// 是否某的同级(同一个上级)
$menu->isSiblingOf($sibling);
  
// 如果$beside不是自己,也不是自己的后代返回true
$menu->isBesideOf($beside);

$menu->delete();



// 清理冗余的关联信息
Menu::deleteRedundancies();
  
$menu = Menu::find(20);
  
// 修复此节点的关联, 它将重新建立 `closure` 表里的记录
$menu->perfectNode();
  
// 修复树关联,注意:这个操作将追朔到到根节点然后从根遍历整颗树调用perfectNode(),如果你的树很庞大将耗费大量资源,请慎用.
// 解决方案是使用队列对每个节点使用 perfectNode()
$menu->perfectTree();




Schema::create('menus', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->unsignedInteger('parent')->default(0);
        });

Schema::create('menu_closure', function (Blueprint $table) {
            $table->unsignedInteger('ancestor');
            $table->unsignedInteger('descendant');
            $table->unsignedTinyInteger('distance');
            $table->primary(['ancestor', 'descendant']);
        });



namespace App;

use Illuminate\Database\Eloquent\Model;
use Jiaxincui\ClosureTable\Traits\ClosureTable;

class Menu extends Model
{
    // (必要)引入ClosureTable.
    use ClosureTable;
 
    // (非必要)关联表名,默认'Model类名+_closure',如'menu_closure'
    protected $closureTable = 'menu_closure';
      
    // (非必要)ancestor列名,默认'ancestor'
    protected $ancestorColumn = 'ancestor';
      
    // (非必要)descendant列名,默认'descendant'
    protected $descendantColumn = 'descendant';
      
    // (非必要)distance列名,默认'distance'
    protected $distanceColumn = 'distance';
      
    // (非必要)parent列名,默认'parent'
    protected $parentColumn = 'parent';
    
}