PHP code example of dcblogdev / laravel-nestable

1. Go to this page and download the library: Download dcblogdev/laravel-nestable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dcblogdev / laravel-nestable example snippets




use Nestable\NestableTrait;

class Category extends \Eloquent {

    use NestableTrait;

    protected $parent = 'parent_id';

}




$categories = Category::nested()->get();



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



Category::renderAsHtml();



Category::attr(['name' => 'categories'])
    ->selected(2)
    ->renderAsDropdown();

->selected([1,2,3])



Category::parent(2)->renderAsArray();



Menu::active('t-shirts')->renderAsHtml();



Menu::active('t-shirts', 'black-t-shirts')->renderAsHtml();



Menu::active(['t-shirts', 'black-t-shirts'])->renderAsHtml();



Menu::active(function($li, $href, $label) {

    $li->addAttr('class', 'active')->addAttr('data-label', $label);

})->renderAsHtml();



Menu::active(function($li, $href, $label) {

    $li->addAttr(['class' => 'active', 'data-label' => $label]);

})->renderAsHtml();



Menu::firstUlAttr('class', 'first-ul')->renderAsHtml();



Menu::firstUlAttr(['class' => 'first-ul'])->renderAsHtml();



Menu::ulAttr('class', 'nav-bar')->renderAsHtml();



Menu::ulAttr(['t-shirts' => 'black-t-shirts'])->renderAsHtml();



Menu::ulAttr(function($ul, $parent_id) {

    if($parent_id == 10) {
        $ul->ulAttr('class', 'nav-bar');
    }

})->renderAsHtml();



Menu::route(['product' => 'slug'])->renderAsHtml();




Route::get('product/{slug}', 'ProductController@show');



Menu::route(function($href, $label, $parent) {

    return \URL::to($href);

})->renderAsHtml();



Menu::customUrl('product/detail/{slug}')->renderAsHtml();



Menu::customUrl('product/{slug}/detail')->renderAsHtml();



Category::selected(1)->renderAsDropdown();



Category::selected(1,5)->renderAsMultiple();



Category::selected([1,3])->renderAsMultiple();



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

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

})->renderAsMultiple();



Category::attr(['name' => 'categories', 'class' => 'red'])->renderAsDropdown();



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



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



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



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



Menu::make($categories)->isValidForHtml();

// return true or false



Menu::make($categories)->isValidForHtml(true);

// return html string if data valid



Nestable::macro('helloWorld', function($nest, $categories) {

    return $nest->make($categories)->active('sweater')->route(['tests' => 'slug'])->renderAsHtml();

});



$categories = [

    [
        'id'        => 1,
        'parent_id' => 0,
        'name'      => 'T-shirt',
        'slug'      => 'T-shirt'
    ],
    [
        'id'        => 2,
        'parent_id' => 0,
        'name'      => 'Sweater',
        'slug'      => 'sweater'
    ]

];

Nestable::helloWorld($categories);



nestable($data)->renderAsHtml();



nestable()->make($data)->renderAsHtml();



nestable()->macro('helloWorld', function() {
    return 'Hello Laravel';
});

// run
nestable()->helloWorld();
ssh
php artisan vendor:publish --provider="Nestable\NestableServiceProvider"