webreinvent / laravel-nestable
Laravel 8嵌套分类库
Requires
- php: ^7.3|^8.0
Requires (Dev)
- orchestra/database: ~3.1
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-04 20:39:53 UTC
README
信息
此仓库是Laravel Nestable的克隆版本,Laravel Nestable已被开发者归档。我们将尽可能维护它以支持Laravel的未来版本。
Laravel Nestable支持递归逻辑。在分类级别没有限制,但这可能取决于您的服务器性能。自PHP 5.2起允许100,000次递归过程执行。更多信息
安装
composer require webreinvent/laravel-nestable
然后
将服务提供者文件添加到app.php。
Nestable\NestableServiceProvider::class
然后再次将外观文件添加到app.php。
'Nestable' => Nestable\Facades\NestableService::class
最后运行artisan命令
php artisan vendor:publish --provider="Nestable\NestableServiceProvider"
就这样!
使用Eloquent的基本用法
假设数据如下所示从数据库中获取。
分类表
示例 1
<?php use Nestable\NestableTrait; class Category extends \Eloquent { use NestableTrait; protected $parent = 'parent_id'; }
注意: $parent变量指的是父分类(默认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])
输出方法
与输出方法一起使用的方法
renderAsArray()
renderAsJson()
renderAsHtml()
renderAsDropdown()/renderAsMultiple()
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文件中的配置变量。
body
body变量应该是数组,并且可以完全自定义。
示例
<?php 'body' => [ 'id', 'category_name', 'category_slug' ]
html
HTML输出的配置。
示例
<?php 'html' => [ 'label' => 'name', 'href' => 'slug', ]
dropdown
下拉列表/列表框输出的配置。
示例
<?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();
验证器
它控制数据的结构。它们也使用第二个参数在它们之后控制渲染过程。
示例 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();