uccello/eloquent-tree
Composer 安装命令:
composer require uccello/eloquent-tree
包简介
Eloquent Tree transforms a model into tree model for Laravel Eloquent ORM.
README 文档
README
Eloquent Tree transforms a model into tree model for Laravel Eloquent ORM.
This project is based on the original project made by Adrian Skierniewski. It was changed to be able to use this functionnality thanks to the IsTree trait, instead of extending the Tree model. It is useful if you want your model extends another class.
Table of Contents
##Features
- Creating root, children and sibling nodes
- Getting children
- Getting descendants
- Getting ancestor
- Moving sub-tree
- Building tree on PHP side
Installation
For Laravel >= 5.3
Begin by installing this package through Composer. Edit your project's composer.json file to require uccello/eloquent-tree.
"require": { "uccello/eloquent-tree": "1.*" }, "minimum-stability" : "stable"
Next, update Composer from the Terminal:
composer update
That's all now you can extend \Gzero\EloquentTree\Model\Tree in your project
Migration
Simply migration with all required columns that you could extend by adding new fields
Schema::create( 'trees', function (Blueprint $table) { $table->increments('id'); $table->string('path', 255)->nullable(); $table->integer('parent_id')->unsigned()->nullable(); $table->integer('level')->default(0); $table->timestamps(); $table->index(array('path', 'parent_id', 'level')); $table->foreign('parent_id')->references('id')->on('contents')->onDelete('CASCADE'); } );
Example usage
- Inserting and Updating new nodes
- Getting tree nodes
- Finding Leaf nodes
- Map from array
- Rendering tree
Inserting and updating new nodes
$root = new Tree(); // New root $root->setAsRoot(); $child = with(new Tree())->setChildOf($root); // New child $sibling = new Tree(); $sibling->setSiblingOf($child); // New sibling
Getting tree nodes
Leaf - returning root node
$leaf->findRoot();
Children - returning flat collection of children. You can use Eloquent query builder.
$collection = $root->children()->get(); $collection2 = $root->children()->where('url', '=', 'slug')->get();
Ancestors - returning flat collection of ancestors, first is root, last is current node. You can use Eloquent query builder. Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where
$collection = $node->findAncestors()->get(); $collection2 = $node->findAncestors()->where('url', '=', 'slug')->get();
Descendants - returning flat collection of descendants, first is current node, last is leafs. You can use Eloquent query builder. Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where
$collection = $node->findDescendants()->get(); $collection2 = $node->findDescendants()->where('url', '=', 'slug')->get();
Building tree structure on PHP side - if some nodes will be missing, these branches will not be built
$treeRoot = $root->buildTree($root->findDescendants()->get())
Getting leaf nodes
Tree::getLeaves();
Map from array
Three new roots, first with descendants
Tree::mapArray( array( array( 'children' => array( array( 'children' => array( array( 'children' => array( array( 'children' => array() ), array( 'children' => array() ) ) ), array( 'children' => array() ) ) ), array( 'children' => array() ) ) ), array( 'children' => array() ), array( 'children' => array() ) ) );
Rendering tree
You can render tree built by the function buildTree
$html = $root->render( 'ul', function ($node) { return '<li>' . $node->title . '{sub-tree}</li>'; }, TRUE ); echo $html;
Events
All tree models have additional events:
- updatingParent
- updatedParent
- updatedDescendants
You can use them for example to update additional tables
Credits
License
The MIT License (MIT). Please see License File for more information.
uccello/eloquent-tree 适用场景与选型建议
uccello/eloquent-tree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 428 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 uccello/eloquent-tree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 uccello/eloquent-tree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 428
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-30