All Projects → atayahmet → Laravel Nestable

atayahmet / Laravel Nestable

Licence: mit
Laravel 5 nested category/menu generator

Projects that are alternatives of or similar to Laravel Nestable

Jkcategories
JKCategories(iOS-Categories,Category), a collection of useful Objective-C Categories extending iOS Frameworks such as Foundation,UIKit,CoreData,QuartzCore,CoreLocation,MapKit Etc.
Stars: ✭ 3,292 (+1424.07%)
Mutual labels:  category
Mbprogresshud Bwmextension
Nihility-Ming to MBProgressHUD extension, easy to use.
Stars: ✭ 35 (-83.8%)
Mutual labels:  category
Zsnavigationbar
ZSNavigationBar uses category to allow you change UINavigationBar appearance dynamically.(supported iOS 11+ and iPhone X)
Stars: ✭ 109 (-49.54%)
Mutual labels:  category
Macproject
这是一个用 Objective-C 写的 iOS 轻量级框架,旨在快速构建 iOS App,欢迎 Star
Stars: ✭ 544 (+151.85%)
Mutual labels:  category
Witchcraft
Monads and other dark magic for Elixir
Stars: ✭ 864 (+300%)
Mutual labels:  category
Hexo Auto Category
Generate categories automatically for each post in Hexo
Stars: ✭ 49 (-77.31%)
Mutual labels:  category
free-category
Free categories, free arrows and free categories with monadic actions
Stars: ✭ 21 (-90.28%)
Mutual labels:  category
Django Treenode
probably the best abstract model / admin for your tree based stuff. 🌳
Stars: ✭ 142 (-34.26%)
Mutual labels:  category
Ios Category
iOS 工具分类整理
Stars: ✭ 30 (-86.11%)
Mutual labels:  category
Yjcategories
Objective-C 常用分类集合,支持Cocoapods
Stars: ✭ 89 (-58.8%)
Mutual labels:  category
Category Theory
An axiom-free formalization of category theory in Coq for personal study and practical work
Stars: ✭ 562 (+160.19%)
Mutual labels:  category
Uiimageview Letters
UIImageView category for using initials as a placeholder image, written in Objective-C. For a Swift implementation, see https://github.com/bachonk/InitialsImageView
Stars: ✭ 694 (+221.3%)
Mutual labels:  category
Lvthemekit
App 多区域皮肤 UIKit Category
Stars: ✭ 75 (-65.28%)
Mutual labels:  category
Smile
😄 Emoji in Swift
Stars: ✭ 359 (+66.2%)
Mutual labels:  category
Channeltagview
一个新闻频道管理view
Stars: ✭ 115 (-46.76%)
Mutual labels:  category
eBay-node-client
Ebay NodeJS Wrapper
Stars: ✭ 50 (-76.85%)
Mutual labels:  category
Depressurizer
A Steam library categorizing tool.
Stars: ✭ 1,008 (+366.67%)
Mutual labels:  category
Bastet
A ReasonML/Ocaml library for category theory and abstract algebra
Stars: ✭ 200 (-7.41%)
Mutual labels:  category
Jxsegmentedview
A powerful and easy to use segmented view (segmentedcontrol, pagingview, pagerview, pagecontrol, categoryview) (腾讯新闻、今日头条、QQ音乐、网易云音乐、京东、爱奇艺、腾讯视频、淘宝、天猫、简书、微博等所有主流APP分类切换滚动视图)
Stars: ✭ 1,905 (+781.94%)
Mutual labels:  category
Sonataclassificationbundle
Symfony SonataClassificationBundle
Stars: ✭ 76 (-64.81%)
Mutual labels:  category

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

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:

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

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

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

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.

<?php

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

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

firstUlAttr()

Add attribute to first ul element

Example 1:

<?php

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

Example 2:

<?php

Menu::firstUlAttr(['class' => 'first-ul'])->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}', '[email protected]');

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

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

Example:

<?php

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

html

Configuration for html output.

name description
label Label column name
href Url column name

Example:

<?php

'html' => [
    '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

'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.

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

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();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].