atayahmet / laravel-nestable
此包已被弃用且不再维护。没有建议的替代包。
Laravel 5 嵌套分类库
v0.8.9
2019-03-20 19:24 UTC
Requires
- php: >=5.5.5
- illuminate/database: 5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*
- illuminate/support: 5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*
Requires (Dev)
- orchestra/database: ~3.1
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^4.0|^5.0
- dev-master
- v0.8.9
- v0.8.8
- v0.8.7
- v0.8.6
- v0.8.5
- v0.8.4
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.37
- v0.6.36
- v0.6.34
- v0.6.33
- v0.6.32
- v0.6.31
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.9
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.8
- v0.2.8
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1-beta1
- v0.1.0-beta1
This package is auto-updated.
Last update: 2020-01-23 07:21:34 UTC
README
Laravel Nestable 支持递归逻辑。在分类级别没有限制,但可能会根据服务器性能而变化。PHP 5.2 允许 100000 次递归过程执行。更多信息请查看 这里
安装
composer require atayahmet/laravel-nestable
然后
将服务提供者文件添加到 app.php。
Nestable\NestableServiceProvider::class
然后再次将外观文件添加到 app.php。
'Nestable' => Nestable\Facades\NestableService::class
最后运行 artisan 命令
php artisan vendor:publish --provider="Nestable\NestableServiceProvider"
完成!
Eloquent 的基本用法
假设数据如下来自数据库。
分类表
id | parent_id | name | slug |
---|---|---|---|
1 | 0 | T-shirts | t-shirts |
2 | 1 | Red T-shirts | red-t-shirts |
3 | 1 | Black T-shirts | black-t-shirts |
4 | 0 | Sweaters | sweaters |
5 | 4 | Red Sweaters | red-sweaters |
6 | 4 | Blue Sweaters | blue-sweaters |
示例 1
<?php use Nestable\NestableTrait; class Category extends \Eloquent { use NestableTrait; protected $parent = 'parent_id'; }
注意: $parent 变量指的是父分类(默认父分类_id)
<?php $categories = Category::nested()->get();
查询结果
<?php array:6 [ 0 => array:5 [ "id" => 1 "name" => "T-shirts" "slug" => "t-shirts" "child" => array:2 [ 0 => array:5 [ "id" => 2 "name" => "Red T-shirts" "slug" => "red-t-shirts" "child" => [] "parent_id" => 1 ] 1 => array:5 [ "id" => 3 "name" => "Black T-shirts" "slug" => "black-t-shirts" "child" => [] "parent_id" => 1 ] ] "parent_id" => 0 ] 1 => array:5 [ "id" => 4 "name" => "Sweaters" "slug" => "sweaters" "child" => array:2 [ 0 => array:5 [ "id" => 5 "name" => "Red Sweaters" "slug" => "red-sweaters" "child" => [] "parent_id" => 4 ] 1 => array:5 [ "id" => 6 "name" => "Blue Sweaters" "slug" => "blue-sweaters" "child" => [] "parent_id" => 4 ] ] "parent_id" => 0 ] ]
用于 HTML 树输出
<?php Category::renderAsHtml();
输出
<ul> <li><a href="">T-shirts <ul> <li><a href="red-t-shirt">Red T-shirt</a></li> <li><a href="black-t-shirts">Black T-shirts</a></li> </ul> </li> <li><a href="">Sweaters <ul> <li><a href="red-sweaters">Red Sweaters</a></li> <li><a href="blue-sweaters">Blue Sweaters</a></li> </ul> </li> </ul>
用于下拉输出
<?php Category::attr(['name' => 'categories']) ->selected(2) ->renderAsDropdown();
输出
<select name="categories"> <option value="1">T-shirts</option> <option value="2" selected="selected"> Red T-shirts</option> <option value="3"> Black T-shirts</option> <option value="4">Sweaters</option> <option value="5"> Red Sweaters</option> <option value="6"> Blue Sweaters</option> </select>
多列表框中选择
->selected([1,2,3])
输出方法
name | 参数 | output |
---|---|---|
renderAsArray() | none | array |
renderAsJson() | none | json |
renderAsHtml() | none | html |
renderAsDropdown() | none | dropdown |
renderAsMultiple() | none | 列表框 |
与输出方法一起使用的可用方法
renderAsArray()
name | 参数 | 描述 |
---|---|---|
parent() | int | 获取指定父类的子类 |
renderAsJson()
name | 参数 | 描述 |
---|---|---|
parent() | int | 获取指定父类的子类 |
renderAsHtml()
name | 参数 | 描述 |
---|---|---|
parent() | int | 获取指定父类的子类 |
active() | callback/array/int | 用于 HTML 输出的选中项 |
ulAttr() | array/string | 为父 ul 元素添加属性 |
firstUlAttr() | array/string | 为父 ul 元素添加属性 |
route() | callback/array | 通过路由名生成 URL |
customUrl() | string | 生成自定义 URL |
renderAsDropdown()/renderAsMultiple()
name | 参数 | 描述 |
---|---|---|
parent() | int | 获取指定父类的子类 |
selected() | callback/array/int | 用于下拉的选中项 |
attr() | array | 下拉/列表框属性 |
parent()
获取指定父类的子类。
<?php Category::parent(2)->renderAsArray();
注意:此方法与所有输出方法兼容
active()
用于 HTML 输出的选中项。
示例 1
<?php Menu::active('t-shirts')->renderAsHtml();
示例 2
<?php Menu::active('t-shirts', 'black-t-shirts')->renderAsHtml();
示例 3
<?php Menu::active(['t-shirts', 'black-t-shirts'])->renderAsHtml();
示例 4
<?php Menu::active(function($li, $href, $label) { $li->addAttr('class', 'active')->addAttr('data-label', $label); })->renderAsHtml();
示例 5
<?php Menu::active(function($li, $href, $label) { $li->addAttr(['class' => 'active', 'data-label' => $label]); })->renderAsHtml();
firstUlAttr()
为第一个ul元素添加属性
示例 1
<?php Menu::firstUlAttr('class', 'first-ul')->renderAsHtml();
示例 2
<?php Menu::firstUlAttr(['class' => 'first-ul'])->renderAsHtml();
ulAttr()
为父 ul 元素添加属性
示例 1
<?php Menu::ulAttr('class', 'nav-bar')->renderAsHtml();
示例 2
<?php Menu::ulAttr(['t-shirts' => 'black-t-shirts'])->renderAsHtml();
示例 3
<?php Menu::ulAttr(function($ul, $parent_id) { if($parent_id == 10) { $ul->ulAttr('class', 'nav-bar'); } })->renderAsHtml();
route()
通过路由名生成 URL
示例 1
<?php Menu::route(['product' => 'slug'])->renderAsHtml();
注意: product 指的是路由名称,而 slug 指的是参数名称。
<?php Route::get('product/{slug}', 'ProductController@show');
示例 2
<?php Menu::route(function($href, $label, $parent) { return \URL::to($href); })->renderAsHtml();
customUrl()
使用slug生成自定义URL
示例 1
<?php Menu::customUrl('product/detail/{slug}')->renderAsHtml();
示例 1
<?php Menu::customUrl('product/{slug}/detail')->renderAsHtml();
注意: slug 关键字属于配置文件中的 html > href。
selected()
下拉菜单中的选中项。
示例 1
<?php Category::selected(1)->renderAsDropdown();
示例 2
<?php Category::selected(1,5)->renderAsMultiple();
示例 3
<?php Category::selected([1,3])->renderAsMultiple();
示例 4
<?php Category::selected(function($option, $value, $label) { $option->addAttr('selected', 'true'); $option->addAttr(['data-item' => $label]); })->renderAsMultiple();
attr()
下拉菜单/列表框属性。
<?php Category::attr(['name' => 'categories', 'class' => 'red'])->renderAsDropdown();
配置
以上示例使用默认设置。在 config/nestable.php 文件中的配置变量。
name | type | 描述 |
---|---|---|
parent | string | 父分类列名 |
主键 | string | 表主键 |
generate_url | 布尔值 | 为HTML输出生成URL |
childNode | string | 子节点名称 |
body | array | 数组输出(默认) |
html | array | Html输出列 |
dropdown | array | 下拉菜单/列表框输出 |
body
body变量应该是一个数组,并且可以完全自定义。
示例
<?php 'body' => [ 'id', 'category_name', 'category_slug' ]
html
HTML输出配置。
name | 描述 |
---|---|
label | 标签列名 |
href | URL列名 |
示例
<?php 'html' => [ 'label' => 'name', 'href' => 'slug', ]
dropdown
下拉菜单/列表框输出配置。
name | 描述 |
---|---|
prefix | 标签前缀 |
label | 标签列名 |
value | 值列名 |
示例
<?php 'dropdown' => [ 'prefix' => '-', 'label' => 'name', 'value' => 'id' ]
使用独立模型
包含Nestable外观。
<?php use Nestable; $result = Nestable::make([ [ 'id' => 1, 'parent_id' => 0, 'name' => 'T-shirts', 'slug' => 't-shirts' ], [ 'id' => 2, 'parent_id' => 1, 'name' => 'Red T-shirts', 'slug' => 'red-t-shirts' ], [ 'id' => 3, 'parent_id' => 1, 'name' => 'Black T-shirts', 'slug' => 'black-t-shirts' ] // and more... ]);
对于数组输出
$result->renderAsArray();
验证器
它控制数据的结构。它们还在第二个参数之后控制了渲染过程。
name | 参数 |
---|---|
isValidForArray | 布尔值 |
isValidForJson | 布尔值 |
isValidForHtml | 布尔值 |
isValidForDropdown | 布尔值 |
isValidForMultiple | 布尔值 |
示例 1
<?php Menu::make($categories)->isValidForHtml(); // return true or false
示例 2
<?php Menu::make($categories)->isValidForHtml(true); // return html string if data valid
宏
<?php Nestable::macro('helloWorld', function($nest, $categories) { return $nest->make($categories)->active('sweater')->route(['tests' => 'slug'])->renderAsHtml(); });
调用上述宏
<?php $categories = [ [ 'id' => 1, 'parent_id' => 0, 'name' => 'T-shirt', 'slug' => 'T-shirt' ], [ 'id' => 2, 'parent_id' => 0, 'name' => 'Sweater', 'slug' => 'sweater' ] ]; Nestable::helloWorld($categories);
助手
<?php nestable($data)->renderAsHtml();
<?php nestable()->make($data)->renderAsHtml();
<?php nestable()->macro('helloWorld', function() { return 'Hello Laravel'; }); // run nestable()->helloWorld();