szwss/grade-tree
Composer 安装命令:
composer require szwss/grade-tree
包简介
grade tree model and observer for laravel 5
README 文档
README
grade tree model and observer for laravel 5
Install
composer require szwss/grade-tree
Configure
在你的项目目录config/app.php的providers数组里加入:
Szwss\GradeTree\GradeTreeProvider::class
使用说明
运行artisan
创建migration:
$ php artisan grade-tree:migration
默认的数据表名是grade_trees,如果需要指定数据表名,需要加上参数--table,例如:
$ php artisan grade-tree:migration --table=mytable
迁移数据表:
$ php artisan migrate
创建模型
使用artisan创建模型,例如:
$ php artisan make:model Cateory
然后,让你的模型use trait GradeTreeTrait
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use \Szwss\GradeTree\GradeTreeTrait;
}
添加模型观察者
在/app/Providers/EventServiceProvider.php的boot方法里添加:
GradeTree::observe(\Szwss\GradeTree\Observer::class);
Example
Create
$parent1 = new Category();
$parent1->name = 'Parent 1';
$parent1->save();
$parent2 = new Category();
$parent2->name = 'Parent 2';
$parent2->save();
$parent3 = new Category();
$parent3->name = 'Parent 3';
$parent3->save();
$parent4 = new Category();
$parent4->name = 'Parent 4';
$parent4->save();
$parent5 = new Category();
$parent5->name = 'Parent 5';
$parent5->save();
$child1 = new Category();
$child1->parent_id = $parent1->id; //把parent_id字段设置为上级分类的id
$child1->name = 'Child 1';
$child1->save();
$child2 = new Category();
$child2->parent_id = $parent1->id;
$child2->name = 'Child 2';
$child2->save();
$child3 = new Category();
$child3->parent_id = $parent1->id;
$child3->name = 'Child 3';
$child3->save();
$grandchild1 = new Category();
$grandchild1->parent_id = $child1->id;
$grandchild1->name = 'Grandchild 1';
$grandchild1->save();
$grandchild2 = new Category();
$grandchild2->parent_id = $child1->id;
$grandchild2->name = 'Grandchild 2';
$grandchild2->save();
结果:
+----+--------+-------+--------------+----------+
| id | parent | level | name | node |
+----+--------+-------+--------------+----------+
| 1 | 0 | 1 | Parent 1 | ,1, |
| 2 | 0 | 1 | Parent 2 | ,2, |
| 3 | 0 | 1 | Parent 3 | ,3, |
| 4 | 0 | 1 | Parent 4 | ,4, |
| 5 | 0 | 1 | Parent 5 | ,5, |
| 6 | 1 | 2 | Child 1 | ,1,6, |
| 7 | 1 | 2 | Child 2 | ,1,7, |
| 8 | 1 | 2 | Child 3 | ,1,8, |
| 9 | 6 | 3 | Grandchild 1 | ,1,6,9, |
| 10 | 6 | 3 | Grandchild 2 | ,1,6,10, |
+----+--------+-------+--------------+----------+
Update parent
$child1 = Category::find(6);
$child1->parent_id = 4; //修改为id为4的子类
$child1->save();
结果:
+----+-----------+-------+--------------+----------+
| id | parent_id | level | name | node |
+----+-----------+-------+--------------+----------+
| 1 | 0 | 1 | Parent 1 | ,1, |
| 2 | 0 | 1 | Parent 2 | ,2, |
| 3 | 0 | 1 | Parent 3 | ,3, |
| 4 | 0 | 1 | Parent 4 | ,4, |
| 5 | 0 | 1 | Parent 5 | ,5, |
| 6 | 4 | 2 | Child 1 | ,4,6, |
| 7 | 1 | 2 | Child 2 | ,1,7, |
| 8 | 1 | 2 | Child 3 | ,1,8, |
| 9 | 6 | 4 | Grandchild 1 | ,4,6,9, |
| 10 | 6 | 4 | Grandchild 2 | ,4,6,10, |
+----+-----------+-------+--------------+----------+
Delete
$parent4 = Category::find(4);
$parent4->delete();
结果:
+----+-----------+-------+----------+-------+
| id | parent_id | level | name | node |
+----+-----------+-------+----------+-------+
| 1 | 0 | 1 | Parent 1 | ,1, |
| 2 | 0 | 1 | Parent 2 | ,2, |
| 3 | 0 | 1 | Parent 3 | ,3, |
| 5 | 0 | 1 | Parent 5 | ,5, |
| 7 | 1 | 2 | Child 2 | ,1,7, |
| 8 | 1 | 2 | Child 3 | ,1,8, |
+----+-----------+-------+----------+-------+
Trait Methods
public \Illuminate\Database\Eloquent\Collection childrens(void)
获取所有子分类
$parent1 = Category::find(1);
dd($parent1->childrens());
public \Illuminate\Database\Eloquent\Collection getParent(void)
获取上级分类
$child1 = Category::find(6);
dd($child1->getParent());
You can use BelongTo Relation
$child1 = Category::find(6);
dd($child1->parent);
public \Illuminate\Database\Eloquent\Collection parents(void)
获取所有父分类
public static array getTree(callable $map = null)
public array tree(callable $map = null)
获取树数据结构
$map callable 处理原始数据的map方法,用于集合
$tree = Category::getTree(function($item) {
$item->title = $item->name;
});
szwss/grade-tree 适用场景与选型建议
szwss/grade-tree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.04k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tree」 「laravel」 「grade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 szwss/grade-tree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 szwss/grade-tree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 szwss/grade-tree 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
jsTree widget for yii2
Doctrine2 behavior traits - slowhop fork
A fast and dynamic Merkle tree implementation
Data structure collections, helpers and utilities for yii2 ActiveRecord models(trees, sorts and etc.)
Salary Grade schedule parser. Look up monthly salaries by grade and step.
Yii2 DynaTree Menu widget uses Dynamic tree view control jquery.dynatree.js
统计信息
- 总下载量: 1.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-01