vergil-lai / node-categories
laravel 5 的 Node categories 模型和观察者
v1.1.4
2017-02-28 09:07 UTC
Requires
- php: >=5.5.9
This package is not auto-updated.
Last update: 2024-09-24 19:47:31 UTC
README
laravel 5 的 Node categories 模型和观察者
安装
composer require vergil-lai/node-categories
配置
在你的项目目录 config/app.php
的 providers
数组里加入
VergilLai\NodeCategories\NodeCategoriesProvider::class
使用说明
运行 artisan
创建迁移并运行 migrate
$ php artisan node-categories:migration
默认的数据表名是 categories
,如果需要指定数据表名,需要加上参数 --table
,例如:
$ php artisan node-categories:migration --table=mytable
创建模型
使用 artisan 创建模型,例如:
$ php artisan make:model Cateory
然后,让你的模型 use trait NodeCategoryTrait
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use \VergilLai\NodeCategories\NodeCategoryTrait;
}
添加模型观察者
在 /app/Providers/EventServiceProvider.php
的 boot
方法里添加:
Category::observe(\VergilLai\NodeCategories\Observer::class);
示例
创建
$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, |
+----+--------+-------+--------------+----------+
更新父分类
$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, |
+----+-----------+-------+--------------+----------+
删除
$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, |
+----+-----------+-------+----------+-------+
特质方法
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());
你可以使用 BelongTo
关系
$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;
});