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();