ultrono/laravel-nestable

Laravel 5.8以上版本嵌套分类库

1.2.1 2024-03-23 08:38 UTC

README

Laravel Nestable支持递归逻辑。分类级别没有限制,但具体取决于服务器的性能。自PHP 5.2起允许执行100000次递归过程。更多信息请参见这里

安装

composer require ultrono/laravel-nestable

如果使用Laravel版本低于5.5,请将服务提供者文件添加到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();