欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

atayahmet/laravel-nestable 多级嵌套 nested nestable laravel many nested category 自己按照这个做的 有大用

atayahmet/laravel-nestable


README

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

Build Status

Install

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!

Basic Usage with Eloquent

Suppose that the data came from a database as follows.

Category table:

idparent_idnameslug
10T-shirtst-shirts
21Red T-shirtsred-t-shirts
31Black T-shirtsblack-t-shirts
40Sweaterssweaters
54Red Sweatersred-sweaters
64Blue Sweatersblue-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])

Output methods

nameParameteroutput
renderAsArray()nonearray
renderAsJson()nonejson
renderAsHtml()nonehtml
renderAsDropdown()nonedropdown
renderAsDropdown()noneListbox

Usable methods with output methods

renderAsArray()

nameparemeterdescription
parent()intGet childs of the defined parent

renderAsJson()

nameparemeterdescription
parent()intGet childs of the defined parent

renderAsHtml()

nameparemeterdescription
parent()intGet childs of the defined parent
active()callback/array/intSelected item(s) for html output
ulAttr()array/stringAdd attribute to parent ul element
route()callback/arrayGenerate url by route name
customUrl()stringGenerate custom url

renderAsDropdown()/renderAsMultiple()

nameparemeterdescription
parent()intGet childs of the defined parent
selected()callback/array/intSelected item(s) for dropdown
attr()arrayDropdown/listbox attributes

parent()

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

active()

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();

ulAttr()

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();

route()

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();

customUrl()

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()

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();

attr()

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.

nametypedescription
parentstringParent category column name
primary_keystringTable primary key
generate_urlbooleanGenerate the url for html output
childNodestringChild node name
bodyarrayArray output (default)
htmlarrayHtml output columns
dropdownarrayDropdown/Listbox output

body

The body variable should be an array and absolutely customizable.

Example:

<?php

'body' => [
    'id',
    'category_name',
    'category_slug'
]

html

Configuration for html output.

namedescription
labelLabel column name
hrefUrl column name

Example:

<?php

'html' => [
    'label' => 'name',
    'href'  => 'slug',
]

dropdown

Configuration for dropdown/listbox output.

namedescription
prefixLabel prefix
labelLabel column name
valueValue column name

Example:

<?php

'dropdown' => [
    'prefix' => '-',
    'label'  => 'name',
    'value'  => 'id'
]

Using Independent Models

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();

Validators

It controls the structure of the data. They also made the rendering process with a second parameter control after they.

nameParameters
isValidForArrayboolean
isValidForJsonboolean
isValidForHtmlboolean
isValidForDropdownboolean
isValidForMultipleboolean

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

Macros

<?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);

Helper

<?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


atayahmet / laravel-nestable

 

Laravel 5嵌套类库

v0.8.02017-05-15 17:53 UTC
 

需要

提示

没有

 

提供

没有

冲突

没有

替代对象

没有

 MIT  91f8d4195ae4a537416b75e6d74751c4865e2068

 

 类别嵌套Laravel嵌套嵌套


读我

Laravel可以使用递归逻辑。类别级别没有限制,但这可能会因服务器性能而异。允许从PHP 5.2执行100000递归过程。更多信息

Build Status

安装

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"

而已!

雄辩的基本用法

假设数据来自数据库,如下所示。

类别表:

IDPARENT_ID名称金属块
10T恤T恤
21红色T恤红T恤
31黑色T恤黑色T恤
40毛衣毛衣
4红色毛衣红毛衣
64蓝色毛衣蓝毛衣

示例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 => array5 [  id  => 3  name  => 黑色T恤 slug  => 黑色T恤小孩=> []  PARENT_ID  => 1           ]         ]  PARENT_ID  => 0       ] 1 => 数组5 [  ID  => 4 名称 => 毛衣金属块 => 毛衣 => 数组2 [ 0 => array5 [  id  => 5  name  =>  Red Sweaters  slug  =>  red-sweater  child  => []  parent_id  => 4           ] 1 = > array5 [  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 >

选择多个列表框:

- >选自([ 123 ])

输出方式

名称参数产量
renderAsArray()没有排列
renderAsJson()没有JSON
renderAsHtml()没有HTML
renderAsDropdown()没有落下
renderAsDropdown()没有列表框

具有输出方法的可用方法

renderAsArray()

名称paremeter描述
父()INT取得定义的父母的孩子

renderAsJson()

名称paremeter描述
父()INT取得定义的父母的孩子

renderAsHtml()

名称paremeter描述
父()INT取得定义的父母的孩子
活性()回调/阵列/ INThtml输出的选定项目
ulAttr()阵列/串将属性添加到父ul元素
路线()回调/阵列按路径名生成网址
customUrl()生成自定义网址

renderAsDropdown()/ renderAsMultiple()

名称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();

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();

路线()

按路径名生成网址

示例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();

customUrl()

用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

类别::选择(15- > renderAsMultiple();

示例3:

<?PHP

类别::选择([ 13 ])- > renderAsMultiple();

示例4:

<?PHP

Category :: selected(function$ option$ value$ label){

    $ option - > addAttr(' selected '' true '); 
$ option - > addAttr([ ' data-item ' => $ label ]);      

})- > renderAsMultiple();

ATTR()

下拉列表/列表框属性。

<?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

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

普通分类: