2017 © Pedro Peláez
 

library laravel-nestable

Laravel 5 nested categories library

image

atayahmet/laravel-nestable

Laravel 5 nested categories library

  • Saturday, March 17, 2018
  • by atayahmet
  • Repository
  • 11 Watchers
  • 179 Stars
  • 7,957 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 38 Forks
  • 0 Open issues
  • 45 Versions
  • 27 % Grown

The README.md

Laravel 5 Nestable

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, (*1)

Build Status, (*2)

Install

composer require atayahmet/laravel-nestable

Then, (*3)

Add to app.php the Service Provider file., (*4)

Nestable\NestableServiceProvider::class

Then add app.php Facade file again., (*5)

'Nestable' => Nestable\Facades\NestableService::class

Finally run the artisan command:, (*6)

php artisan vendor:publish --provider="Nestable\NestableServiceProvider"

That's it!, (*7)

Basic Usage with Eloquent

Suppose that the data came from a database as follows., (*8)

Category table:, (*9)

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:, (*10)

<?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), (*11)

<?php

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

Query result:, (*12)

<?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:, (*13)

<?php

Category::renderAsHtml();

Output:, (*14)




For dropdown output:, (*15)

<?php

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

Output:, (*16)

<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:, (*17)

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

Output methods

name Parameter output
renderAsArray() none array
renderAsJson() none json
renderAsHtml() none html
renderAsDropdown() none dropdown
renderAsMultiple() none Listbox

Usable methods with output methods

renderAsArray()

name paremeter description
parent() int Get childs of the defined parent

renderAsJson()

name paremeter description
parent() int Get childs of the defined parent

renderAsHtml()

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
firstUlAttr() array/string Add attribute to parent ul element
route() callback/array Generate url by route name
customUrl() string Generate custom url

renderAsDropdown()/renderAsMultiple()

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

parent()

Get childs of the defined parent., (*18)

<?php

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

Note: This methods usable all with output methods, (*19)

active()

Selected item(s) for html output., (*20)

Example 1:, (*21)

<?php

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

Example 2:, (*22)

<?php

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

Example 3:, (*23)

<?php

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

Example 4:, (*24)

<?php

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

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

})->renderAsHtml();

Example 5:, (*25)

<?php

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

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

})->renderAsHtml();

firstUlAttr()

Add attribute to first ul element, (*26)

Example 1:, (*27)

<?php

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

Example 2:, (*28)

<?php

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

ulAttr()

Add attribute to parent ul element, (*29)

Example 1:, (*30)

<?php

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

Example 2:, (*31)

<?php

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

Example 3:, (*32)

<?php

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

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

})->renderAsHtml();

route()

Generate url by route name, (*33)

Example 1:, (*34)

<?php

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

Note: product refer to route name and slug refer to paremeter name., (*35)

<?php

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

Example 2:, (*36)

<?php

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

    return \URL::to($href);

})->renderAsHtml();

customUrl()

Generate custom url with slug, (*37)

Example 1:, (*38)

<?php

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

Example 1:, (*39)

<?php

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

Note: slug keyword belongs to html > href in config file., (*40)

selected()

Selected item(s) for dropdown., (*41)

Example 1:, (*42)

<?php

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

Example 2:, (*43)

<?php

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

Example 3:, (*44)

<?php

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

Example 4:, (*45)

<?php

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

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

})->renderAsMultiple();

attr()

Dropdown/listbox attributes., (*46)

<?php

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

Configuration

The above examples were performed with default settings. Config variables in config/nestable.php file., (*47)

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

body

The body variable should be an array and absolutely customizable., (*48)

Example:, (*49)

<?php

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

html

Configuration for html output., (*50)

name description
label Label column name
href Url column name

Example:, (*51)

<?php

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

Configuration for dropdown/listbox output., (*52)

name description
prefix Label prefix
label Label column name
value Value column name

Example:, (*53)

<?php

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

Using Independent Models

Include the Nestable facade., (*54)

<?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:, (*55)

$result->renderAsArray();

Validators

It controls the structure of the data. They also made the rendering process with a second parameter control after they., (*56)

name Parameters
isValidForArray boolean
isValidForJson boolean
isValidForHtml boolean
isValidForDropdown boolean
isValidForMultiple boolean

Example 1:, (*57)

<?php

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

// return true or false

Example 2:, (*58)

<?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:, (*59)

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

The Versions

17/03 2018

dev-master

9999999-dev https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

18/02 2018
21/12 2017

v0.8.6

0.8.6.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

20/12 2017

v0.8.5

0.8.5.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

12/12 2017

v0.8.4

0.8.4.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

05/12 2017

v0.8.3

0.8.3.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

04/12 2017

v0.8.2

0.8.2.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

03/09 2017

v0.8.1

0.8.1.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

03/09 2017

v0.8.0

0.8.0.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

07/02 2017

v0.7.3

0.7.3.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

11/01 2017

v0.7.2

0.7.2.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

25/12 2016

v0.7.1

0.7.1.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

21/12 2016

v0.7.0

0.7.0.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

09/09 2016

v0.6.37

0.6.37.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

08/09 2016

v0.6.36

0.6.36.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

08/09 2016

v0.6.34

0.6.34.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

18/08 2016

v0.6.33

0.6.33.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

18/08 2016

v0.6.32

0.6.32.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

22/07 2016

v0.6.31

0.6.31.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

23/06 2016

v0.6.3

0.6.3.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

14/06 2016

v0.6.2

0.6.2.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

13/06 2016

v0.6.1

0.6.1.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

06/06 2016

v0.6.0

0.6.0.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

23/05 2016

v0.5.0

0.5.0.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

23/05 2016

v0.4.9

0.4.9.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel nestedset nested category nestable

23/05 2016

v0.4.8

0.4.8.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

23/05 2016

v0.4.7

0.4.7.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

23/05 2016

v0.4.5

0.4.5.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

23/05 2016

v0.4.6

0.4.6.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

22/05 2016

v0.4.4

0.4.4.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

19/04 2016

v0.4.3

0.4.3.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

19/04 2016

v0.4.2

0.4.2.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

17/04 2016

v0.4.1

0.4.1.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

14/04 2016

v0.4.0

0.4.0.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

13/04 2016

v0.3.8

0.3.8.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

10/04 2016

v0.2.8

0.2.8.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

09/04 2016

v0.1.8

0.1.8.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

09/04 2016

v0.1.7

0.1.7.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

07/04 2016

v0.1.6

0.1.6.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

07/04 2016

v0.1.5

0.1.5.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

07/04 2016

v0.1.4

0.1.4.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

07/04 2016

v0.1.3

0.1.3.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

07/04 2016

v0.1.1-beta1

0.1.1.0-beta1 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

07/04 2016

v0.1.2

0.1.2.0 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable

07/04 2016

v0.1.0-beta1

0.1.0.0-beta1 https://github.com/atayahmet/nestable

Laravel 5 nested categories library

  Sources   Download

MIT

The Requires

 

laravel nestedset nested category nestable