Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/atayahmet/laravel-nestable

Laravel 5 nested category/menu generator
https://github.com/atayahmet/laravel-nestable

category html laravel-nestable nested-hierarchies nested-menus nestedset php

Last synced: about 1 month ago
JSON representation

Laravel 5 nested category/menu generator

Awesome Lists containing this project

README

        

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](http://php.net/manual/en/pcre.configuration.php#ini.pcre.recursion-limit)

[![Build Status](https://travis-ci.org/atayahmet/laravel-nestable.svg?branch=master)](https://travis-ci.org/atayahmet/laravel-nestable)

Install
---

```ssh
composer require atayahmet/laravel-nestable
```

Then

Add to **app.php** the Service Provider file.
```ssh
Nestable\NestableServiceProvider::class
```

Then add **app.php** Facade file again.
```ssh
'Nestable' => Nestable\Facades\NestableService::class
```

Finally run the artisan command:
```ssh
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:

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
**Note**: **$parent** variable refers to the parent category (Default parent_id)

```php
get();
```
Query result:

```php
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

  • T-shirts

  • Sweaters

  • ```

    For dropdown output:

    ```php
    'categories'])
    ->selected(2)
    ->renderAsDropdown();
    ```

    Output:

    ```html

    T-shirts
    Red T-shirts
    Black T-shirts

    Sweaters
    Red Sweaters
    Blue Sweaters

    ```

    Selected for multiple list box:
    ```php
    ->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()](#parent) | int | Get childs of the defined parent

    renderAsJson()
    ---
    name | paremeter| description
    ----------------------| -------- | --------------------------------
    [parent()](#parent) | int | Get childs of the defined parent

    renderAsHtml()
    ---
    name | paremeter | description
    ----------------------| ----------------- | --------------------------------
    [parent()](#parent) | int | Get childs of the defined parent
    [active()](#active) | callback/array/int| Selected item(s) for html output
    [ulAttr()](#ulAttr) | array/string | Add attribute to parent ul element
    [firstUlAttr()](#firstUlAttr) | array/string | Add attribute to parent ul element
    [route()](#route) | callback/array | Generate url by route name
    [customUrl()](#custom-url) | string | Generate custom url

    renderAsDropdown()/renderAsMultiple()
    ---
    name | paremeter | description
    ----------------------| ----------------- | --------------------------------
    [parent()](#parent) | int | Get childs of the defined parent
    [selected()](#selected)| callback/array/int| Selected item(s) for dropdown
    [attr()](#attr) | array | Dropdown/listbox attributes

    parent()
    ---
    Get childs of the defined parent.

    ```php
    renderAsArray();
    ```
    >**Note:** This methods usable all with output methods

    active()
    ---
    Selected item(s) for html output.

    Example 1:
    ```php
    renderAsHtml();
    ```

    Example 2:
    ```php
    renderAsHtml();
    ```

    Example 3:
    ```php
    renderAsHtml();
    ```

    Example 4:
    ```php
    addAttr('class', 'active')->addAttr('data-label', $label);

    })->renderAsHtml();
    ```

    Example 5:
    ```php
    addAttr(['class' => 'active', 'data-label' => $label]);

    })->renderAsHtml();
    ```

    firstUlAttr()
    ---
    Add attribute to first ul element

    Example 1:
    ```php
    renderAsHtml();
    ```

    Example 2:
    ```php
    'first-ul'])->renderAsHtml();
    ```

    ulAttr()
    ---
    Add attribute to parent ul element

    Example 1:
    ```php
    renderAsHtml();
    ```

    Example 2:
    ```php
    'black-t-shirts'])->renderAsHtml();
    ```

    Example 3:
    ```php
    ulAttr('class', 'nav-bar');
    }

    })->renderAsHtml();
    ```

    route()
    ---

    Generate url by route name

    Example 1:
    ```php
    'slug'])->renderAsHtml();

    ```
    >**Note:** **product** refer to route name and **slug** refer to paremeter name.

    ```php
    renderAsHtml();
    ```

    customUrl()
    ---
    Generate custom url with slug

    Example 1:
    ```php
    renderAsHtml();
    ```

    Example 1:
    ```php
    renderAsHtml();
    ```

    >**Note:** **slug** keyword belongs to html > href in config file.

    selected()
    ---
    Selected item(s) for dropdown.

    Example 1:
    ```php
    renderAsDropdown();
    ```

    Example 2:
    ```php
    renderAsMultiple();
    ```

    Example 3:
    ```php
    renderAsMultiple();
    ```

    Example 4:
    ```php
    addAttr('selected', 'true');
    $option->addAttr(['data-item' => $label]);

    })->renderAsMultiple();
    ```

    attr()
    ---
    Dropdown/listbox attributes.

    ```php
    'categories', 'class' => 'red'])->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](#body) | array | Array output (default)
    [html](#html) | array | Html output columns
    [dropdown](#dropdown) | array | Dropdown/Listbox output

    body
    ---

    The body variable should be an array and absolutely customizable.

    Example:
    ```php
    [
    'id',
    'category_name',
    'category_slug'
    ]
    ```

    html
    ----

    Configuration for html output.

    name | description
    -------------| -----------------
    label | Label column name
    href | Url column name

    Example:
    ```php
    [
    'label' => 'name',
    'href' => 'slug',
    ]
    ```

    dropdown
    ----

    Configuration for dropdown/listbox output.

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

    Example:

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

    Using Independent Models
    ---

    Include the Nestable facade.

    ```php
    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:
    ```php
    $result->renderAsArray();
    ```

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

    // return true or false
    ```

    **Example 2:**
    ```php
    isValidForHtml(true);

    // return html string if data valid
    ```

    Macros
    ---

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

    });
    ```

    Call the above macro:

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

    ];

    Nestable::helloWorld($categories);
    ```

    Helper
    ---

    ```php
    renderAsHtml();
    ```

    ```php
    make($data)->renderAsHtml();
    ```

    ```php
    macro('helloWorld', function() {
    return 'Hello Laravel';
    });

    // run
    nestable()->helloWorld();
    ```