欢迎各位兄弟 发布技术文章
这里的技术是共享的
Laravel 5 nested categories library
requires
php: >=5.5.5
illuminate/support: 5.2.*|5.3.*|5.4.*
illuminate/database: 5.2.*|5.3.*|5.4.*
requires (dev)
phpunit/phpunit: ^4.0|^5.0
orchestra/testbench: ~3.0
orchestra/database: ~3.1
suggests
None
provides
None
conflicts
None
replaces
None
Laravel Nestable to work with recursive logic. Category level there is no limit but this may vary depending on your server performance. Allow the 100000 recursion process execution since PHP 5.2. More info
composer require atayahmet/laravel-nestable
Then
Add to app.php the Service Provider file.
Nestable\NestableServiceProvider::class
Then add app.php Facade file again.
'Nestable' => Nestable\Facades\NestableService::class
Finally run the artisan command:
php artisan vendor:publish --provider="Nestable\NestableServiceProvider"
That's it!
Suppose that the data came from a database as follows.
Category table:
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 |
Example 1:
<?php use Nestable\NestableTrait; class Category extends \Eloquent { use NestableTrait; protected $parent = 'parent_id'; }
Note: $parent variable refers to the parent category (Default parent_id)
<?php $categories = Category::nested()->get(); //好像 Category::nested() 是引用如果程序前面用到的话 可能数据不对 //加上 Category::nested()->parent(0) 也不行,好像每次 都是取的 第一次的 Category::nested() 的数据
$categories = Category::nested()->parent(0)->get();
Query result:
<?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 ] ]
For html tree output:
<?php Category::renderAsHtml();
Output:
<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>
For dropdown output:
<?php Category::attr(['name' => 'categories']) ->selected(2) ->renderAsDropdown();
Output:
<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 for multiple list box:
->selected([1,2,3])
name | Parameter | output |
---|---|---|
renderAsArray() | none | array |
renderAsJson() | none | json |
renderAsHtml() | none | html |
renderAsDropdown() | none | dropdown |
renderAsDropdown() | none | Listbox |
name | paremeter | description |
---|---|---|
parent() | int | Get childs of the defined parent |
name | paremeter | description |
---|---|---|
parent() | int | Get childs of the defined parent |
name | paremeter | description |
---|---|---|
parent() | int | Get childs of the defined parent |
active() | callback/array/int | Selected item(s) for html output |
ulAttr() | array/string | Add attribute to parent ul element |
route() | callback/array | Generate url by route name |
customUrl() | string | Generate custom url |
name | paremeter | description |
---|---|---|
parent() | int | Get childs of the defined parent |
selected() | callback/array/int | Selected item(s) for dropdown |
attr() | array | Dropdown/listbox attributes |
Get childs of the defined parent.
<?php Category::parent(2)->renderAsArray();
Category::parent($parent_id)->orderby('category_sort','desc')->renderAsArray();
也可以使用 $sonCategorys = Category::nested()->parent($parent_id)->orderby('category_sort','desc')->get();Note: This methods usable all with output methods
Selected item(s) for html output.
Example 1:
<?php Menu::active('t-shirts')->renderAsHtml();
Example 2:
<?php Menu::active('t-shirts', 'black-t-shirts')->renderAsHtml();
Example 3:
<?php Menu::active(['t-shirts', 'black-t-shirts'])->renderAsHtml();
Example 4:
<?php Menu::active(function($li, $href, $label) { $li->addAttr('class', 'active')->addAttr('data-label', $label); })->renderAsHtml();
Example 5:
<?php Menu::active(function($li, $href, $label) { $li->addAttr(['class' => 'active', 'data-label' => $label]); })->renderAsHtml();
Add attribute to parent ul element
Example 1:
<?php Menu::ulAttr('class', 'nav-bar')->renderAsHtml();
Example 2:
<?php Menu::ulAttr(['t-shirts' => 'black-t-shirts'])->renderAsHtml();
Example 3:
<?php Menu::ulAttr(function($ul, $parent_id) { if($parent_id == 10) { $ul->ulAttr('class', 'nav-bar'); } })->renderAsHtml();
Generate url by route name
Example 1:
<?php Menu::route(['product' => 'slug'])->renderAsHtml();
Note: product refer to route name and slug refer to paremeter name.
<?php Route::get('product/{slug}', 'ProductController@show');
Example 2:
<?php Menu::route(function($href, $label, $parent) { return \URL::to($href); })->renderAsHtml();
Generate custom url with slug
Example 1:
<?php Menu::customUrl('product/detail/{slug}')->renderAsHtml();
Example 1:
<?php Menu::customUrl('product/{slug}/detail')->renderAsHtml();
Note: slug keyword belongs to html > href in config file.
Selected item(s) for dropdown.
Example 1:
<?php Category::selected(1)->renderAsDropdown();
Example 2:
<?php Category::selected(1,5)->renderAsMultiple();
Example 3:
<?php Category::selected([1,3])->renderAsMultiple();
Example 4:
<?php Category::selected(function($option, $value, $label) { $option->addAttr('selected', 'true'); $option->addAttr(['data-item' => $label]); })->renderAsMultiple();
Dropdown/listbox attributes.
<?php Category::attr(['name' => 'categories', 'class' => 'red'])->renderAsDropdown();
//第一个放请选择 {!! \App\Category::attr(['name' => 'parent_id', 'class' => 'form-control industry_small_id_select'])-> selected(1)->placeholder('0','--请选择--') ->renderAsDropdown() !!}Configuration
The above examples were performed with default settings. Config variables in config/nestable.php file.
name | type | description |
---|---|---|
parent | string | Parent category column name |
primary_key | string | Table primary key |
generate_url | boolean | Generate the url for html output |
childNode | string | Child node name |
body | array | Array output (default) |
html | array | Html output columns |
dropdown | array | Dropdown/Listbox output |
The body variable should be an array and absolutely customizable.
Example:
<?php 'body' => [ 'id', 'category_name', 'category_slug' ]
Configuration for html output.
name | description |
---|---|
label | Label column name |
href | Url column name |
Example:
<?php 'html' => [ 'label' => 'name', 'href' => 'slug', ]
Configuration for dropdown/listbox output.
name | description |
---|---|
prefix | Label prefix |
label | Label column name |
value | Value column name |
Example:
<?php 'dropdown' => [ 'prefix' => '-', 'label' => 'name', 'value' => 'id' ]
Include the Nestable facade.
<?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... ]);
For array output:
$result->renderAsArray();
It controls the structure of the data. They also made the rendering process with a second parameter control after they.
name | Parameters |
---|---|
isValidForArray | boolean |
isValidForJson | boolean |
isValidForHtml | boolean |
isValidForDropdown | boolean |
isValidForMultiple | boolean |
Example 1:
<?php Menu::make($categories)->isValidForHtml(); // return true or false
Example 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(); });
Call the above macro:
<?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();
来自 https://packagist.org/packages/atayahmet/laravel-nestable
Laravel可以使用递归逻辑。类别级别没有限制,但这可能会因服务器性能而异。允许从PHP 5.2执行100000递归过程。更多信息
composer require atayahmet/laravel-nestable
然后
添加到app.php服务提供程序文件。
Nestable\NestableServiceProvider::class
然后再次添加app.php Facade文件。
'Nestable' => Nestable\Facades\NestableService::class
最后运行工匠指令:
php artisan vendor:publish --provider="Nestable\NestableServiceProvider"
而已!
假设数据来自数据库,如下所示。
类别表:
ID | PARENT_ID | 名称 | 金属块 |
---|---|---|---|
1 | 0 | T恤 | T恤 |
2 | 1 | 红色T恤 | 红T恤 |
3 | 1 | 黑色T恤 | 黑色T恤 |
4 | 0 | 毛衣 | 毛衣 |
五 | 4 | 红色毛衣 | 红毛衣 |
6 | 4 | 蓝色毛衣 | 蓝毛衣 |
示例1:
<?PHP 使用 Nestable \ NestableTrait ; 一流的 范畴 扩展 \ 雄辩 { 使用 NestableTrait ; protected $ parent = ' parent_id ' ; }
注意:$ parent变量指的是父类(默认parent_id)
<?PHP $ categories = Category :: nested()- > get();
查询结果:
<?PHP 阵列:6 [ 0 => 数组:5 [ “ ID ” => 1 “名称” => “的T恤衫” “金属块” => “的T恤。” “孩子” => 数组:2 [ 0 => 数组:5 [ “ id ” => 2 “ name ” => “红色T恤“ ” slug “ => ” red-t-shirt “ ” child “ => [] ” parent_id “ => 1 ] 1 => array:5 [ ” id “ => 3 ” name “ => ”黑色T恤“ ” slug “ => ”黑色T恤“ ”小孩“=> [] “ PARENT_ID ” => 1 ] ] “ PARENT_ID ” => 0 ] 1 => 数组:5 [ “ ID ” => 4 “名称” => “毛衣” “金属块” => “毛衣” “子“ => 数组:2 [ 0 => array:5 [ “ id ” => 5 “ name ” => “ Red Sweaters ” “ slug ” => “ red-sweater ” “ child ” => [] “ parent_id ” => 4 ] 1 = > array:5 [ “ id ” =>6 “ name ” => “ Blue Sweaters ” “ slug ” => “ blue-sweater ” “ child ” => [] “ parent_id ” => 4 ] ] “ parent_id ” => 0 ] ]“ blue-sweater ” “ child ” => [] “ parent_id ” => 4 ] ] “ parent_id ” => 0 ] ]“ blue-sweater ” “ child ” => [] “ parent_id ” => 4 ] ] “ parent_id ” => 0 ] ]
对于html树输出:
<?PHP 类别:: renderAsHtml();
输出:
< ul > < 锂 > < 一个 HREF = “ ” > T恤衫 < ul > < 锂 > < 一个 HREF = “红T恤” >红色T恤衫</ 一 > </ 李 > < 锂 > < 一个 HREF = “黑T恤” >黑色T恤</ 一 > </ 李 > </ ul > </ li > < 锂 > < 一个 HREF = “ ” >毛衣 < ul > < 锂 > < 一个 HREF = “红毛衣” >红色毛衣</ 一 > </ 李 > < 锂 > < 一个 HREF = “蓝毛衣” >蓝毛衣</ 一 > </ 李 > </ ul > </ li > </ ul >
对于下拉式输出:
<?PHP 类别:: attr([ ' name ' => ' categories ' ]) - > selected(2)- > renderAsDropdown();
输出:
< select name = “ categories ” > < 选项 值 = “ 1 ” > T恤</ option > < option value = “ 2 ” selected = “ selected ” >红色T恤</ option > < 选项 值 = “ 3 ” >黑色T恤</ option > < 选项 值 = “ 4 ” >毛衣</ option > < 选项 值 = “ 5 ” >红色毛衣</ option > < 选项 值 = “ 6 ” >蓝色毛衣</ option > </ select >
选择多个列表框:
- >选自([ 1,2,3 ])
名称 | 参数 | 产量 |
---|---|---|
renderAsArray() | 没有 | 排列 |
renderAsJson() | 没有 | JSON |
renderAsHtml() | 没有 | HTML |
renderAsDropdown() | 没有 | 落下 |
renderAsDropdown() | 没有 | 列表框 |
名称 | paremeter | 描述 |
---|---|---|
父() | INT | 取得定义的父母的孩子 |
名称 | paremeter | 描述 |
---|---|---|
父() | INT | 取得定义的父母的孩子 |
名称 | paremeter | 描述 |
---|---|---|
父() | INT | 取得定义的父母的孩子 |
活性() | 回调/阵列/ INT | html输出的选定项目 |
ulAttr() | 阵列/串 | 将属性添加到父ul元素 |
路线() | 回调/阵列 | 按路径名生成网址 |
customUrl() | 串 | 生成自定义网址 |
名称 | paremeter | 描述 |
---|---|---|
父() | INT | 取得定义的父母的孩子 |
选择() | 回调/阵列/ INT | 所选项目用于下拉菜单 |
ATTR() | 排列 | 下拉列表/列表框属性 |
取得定义的父母的孩子。
<?PHP Category :: parent(2)- > renderAsArray();
Category::parent($parent_id)->orderby('category_sort','desc')->renderAsArray();
也可以使用 $sonCategorys = Category::nested()->parent($parent_id)->orderby('category_sort','desc')->get();注意:这种方法可以使用所有输出方法
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();
将属性添加到父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();
按路径名生成网址
示例1:
<?PHP Menu :: route([ ' product ' => ' slug ' ])- > renderAsHtml();
注: 产品是指路由名称和蛞蝓是指paremeter名。
<?PHP Route :: get(' product / {slug} ',' ProductController @ show ');
示例2:
<?PHP Menu :: route(function($ href,$ label,$ parent){ return \ URL :: to($ href); })- > renderAsHtml();
用slug生成自定义url
示例1:
<?PHP Menu :: customUrl(' product / detail / {slug} ')- > renderAsHtml();
示例1:
<?PHP Menu :: customUrl(' product / {slug} / detail ')- > renderAsHtml();
注意: slug关键字属于配置文件中的html> href。
所选项目用于下拉菜单。
示例1:
<?PHP 类别:: selected(1)- > renderAsDropdown();
示例2:
<?PHP 类别::选择(1,5)- > renderAsMultiple();
示例3:
<?PHP 类别::选择([ 1,3 ])- > renderAsMultiple();
示例4:
<?PHP Category :: selected(function($ option,$ value,$ label){ $ option - > addAttr(' selected ',' true '); $ option - > addAttr([ ' data-item ' => $ label ]); })- > renderAsMultiple();
下拉列表/列表框属性。
<?PHP 类别:: attr([ ' name ' => ' categories ',' class ' => ' red ' ])- > renderAsDropdown();
//第一个放请选择 {!! \App\Category::attr(['name' => 'parent_id', 'class' => 'form-control industry_small_id_select'])-> selected(1)->placeholder('0','--请选择--') ->renderAsDropdown() !!}组态
上述示例使用默认设置进行。config / nestable.php文件中的配置变量。
名称 | 类型 | 描述 |
---|---|---|
亲 | 串 | 父类别列名称 |
首要的关键 | 串 | 表主键 |
generate_url | 布尔 | 生成html输出的url |
childNode | 串 | 子节点名称 |
身体 | 排列 | 阵列输出(默认) |
HTML | 排列 | Html输出列 |
落下 | 排列 | 下拉菜单/列表框输出 |
body变量应该是一个数组,绝对可定制。
例:
<?PHP ' body ' => [ ' id ', ' category_name ', ' category_slug ' ]
html输出的配置。
名称 | 描述 |
---|---|
标签 | 标签列名称 |
HREF | 网址栏名称 |
例:
<?PHP ' html ' => [ ' label ' => ' name ', ' href ' => ' slug ',]
下拉菜单/列表框输出配置。
名称 | 描述 |
---|---|
字首 | 标签前缀 |
标签 | 标签列名称 |
值 | 值列名称 |
例:
<?PHP '下拉' => [ '前缀' => ' - ', '标签' => '名称', '值' => ' ID ' ]
包括Nestable门面。
<?PHP 使用 Nestable ; $ result = Nestable :: make([ [ ' id ' => 1,' parent_id ' => 0,' name ' => ' T恤',' slug ' => ' t-shirts ' ], [ ' id ' => 2,' parent_id ' => 1,' name ' => '红色T恤衫' '蛞蝓' => '红色T恤衫' ], [ ' ID ' => 3,' PARENT_ID ' => 1,'名称' => '黑色T恤' '蛞蝓' => ' black-t-shirt ' ] // and more ... ]);'红T恤' ],[ ' ID ' => 3,' PARENT_ID ' => 1,'名称' => '黑色T恤','蛞蝓' => '黑T恤' ] / /和更多... ]);'红T恤' ],[ ' ID ' => 3,' PARENT_ID ' => 1,'名称' => '黑色T恤','蛞蝓' => '黑T恤' ] / /和更多... ]);=> 1,' name ' => '黑色T恤',' slug ' => ' black-t-shirt ' ] // and more ... ]);=> 1,' name ' => '黑色T恤',' slug ' => ' black-t-shirt ' ] // and more ... ]);
对于阵列输出:
$ result - > renderAsArray();
它控制数据的结构。他们还在渲染过程之后进行了第二个参数控制。
名称 | 参数 |
---|---|
isValidForArray | 布尔 |
isValidForJson | 布尔 |
isValidForHtml | 布尔 |
isValidForDropdown | 布尔 |
isValidForMultiple | 布尔 |
示例1:
<?PHP Menu :: make($ categories)- > isValidForHtml(); //返回true或false
示例2:
<?PHP Menu :: make($ categories)- > isValidForHtml(true); //如果数据有效,则返回html字符串
<?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恤',' slug ' => ' T恤' ], [ ' id ' => 2,' parent_id ' = > 0,' name ' => '毛衣',' slug '=> '毛衣' ] ]。 Nestable :: helloWorld($ categories);
<?PHP 可嵌套($ data)- > renderAsHtml();
<?PHP 可嵌套()- > make($ data)- > renderAsHtml();
<?PHP 可嵌套()- >宏('的helloWorld ',函数(){ 返回'你好Laravel ' ; }); //运行 可嵌套() - >的helloWorld();
来自 https://packagist.org/packages/atayahmet/laravel-nestable
来自 https://github.com/atayahmet/laravel-nestable