{"id":15823101,"url":"https://github.com/dcblogdev/laravel-nestable","last_synced_at":"2025-04-01T17:33:00.852Z","repository":{"id":56964052,"uuid":"235794333","full_name":"dcblogdev/laravel-nestable","owner":"dcblogdev","description":"Laravel 5 nested category/menu generator","archived":false,"fork":false,"pushed_at":"2024-03-12T11:11:58.000Z","size":68,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-12T08:31:01.581Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcblogdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["dcblogdev"]}},"created_at":"2020-01-23T12:52:07.000Z","updated_at":"2023-03-09T00:50:03.000Z","dependencies_parsed_at":"2024-10-26T12:49:29.726Z","dependency_job_id":"488df674-9b14-434c-bcde-9792e9632156","html_url":"https://github.com/dcblogdev/laravel-nestable","commit_stats":{"total_commits":83,"total_committers":13,"mean_commits":6.384615384615385,"dds":"0.40963855421686746","last_synced_commit":"de0ade989a55f8eb8325fd204cecf136857dcdee"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcblogdev%2Flaravel-nestable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcblogdev%2Flaravel-nestable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcblogdev%2Flaravel-nestable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcblogdev%2Flaravel-nestable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcblogdev","download_url":"https://codeload.github.com/dcblogdev/laravel-nestable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246680516,"owners_count":20816707,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T08:04:09.676Z","updated_at":"2025-04-01T17:33:00.575Z","avatar_url":"https://github.com/dcblogdev.png","language":"PHP","funding_links":["https://github.com/sponsors/dcblogdev"],"categories":[],"sub_categories":[],"readme":"Laravel Nestable\n========\n\nForked from https://github.com/atayahmet/laravel-nestable due to being archived and needing the package to support Laravel 6. Thanks to Ahmet for this package.\n\nLaravel Nestable to work with recursive logic. Category level there is no limit but\nthis 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)\n\nInstall\n---\n\n```ssh\ncomposer require dcblogdev/laravel-nestable\n```\n\nThen\n\nAdd to **app.php** the Service Provider file.\n```ssh\nNestable\\NestableServiceProvider::class\n```\n\nThen add **app.php** Facade file again.\n```ssh\n'Nestable' =\u003e Nestable\\Facades\\NestableService::class\n```\n\nFinally run the artisan command:\n```ssh\nphp artisan vendor:publish --provider=\"Nestable\\NestableServiceProvider\"\n```\nThat's it!\n\nBasic Usage with Eloquent\n---\n\nSuppose that the data came from a database as follows.\n\nCategory table:\n\nid | parent_id | name           | slug\n---| --------- | -------------- | -------\n1  | 0         | T-shirts       | t-shirts\n2  | 1         | Red T-shirts   | red-t-shirts\n3  | 1         | Black T-shirts | black-t-shirts\n4  | 0         | Sweaters       | sweaters\n5  | 4         | Red Sweaters   | red-sweaters\n6  | 4         | Blue Sweaters  | blue-sweaters\n\nExample 1:\n\n```php\n\u003c?php\n\nuse Nestable\\NestableTrait;\n\nclass Category extends \\Eloquent {\n\n    use NestableTrait;\n\n    protected $parent = 'parent_id';\n\n}\n\n```\n\n\u003e**Note**: **$parent** variable refers to the parent  category (Default parent_id)\n\n\n```php\n\u003c?php\n\n$categories = Category::nested()-\u003eget();\n```\nQuery result:\n\n```php\n\u003c?php\n\narray:6 [\n      0 =\u003e array:5 [\n        \"id\" =\u003e 1\n        \"name\" =\u003e \"T-shirts\"\n        \"slug\" =\u003e \"t-shirts\"\n        \"child\" =\u003e array:2 [\n          0 =\u003e array:5 [\n            \"id\" =\u003e 2\n            \"name\" =\u003e \"Red T-shirts\"\n            \"slug\" =\u003e \"red-t-shirts\"\n            \"child\" =\u003e []\n            \"parent_id\" =\u003e 1\n          ]\n          1 =\u003e array:5 [\n            \"id\" =\u003e 3\n            \"name\" =\u003e \"Black T-shirts\"\n            \"slug\" =\u003e \"black-t-shirts\"\n            \"child\" =\u003e []\n            \"parent_id\" =\u003e 1\n          ]\n        ]\n        \"parent_id\" =\u003e 0\n      ]\n      1 =\u003e array:5 [\n        \"id\" =\u003e 4\n        \"name\" =\u003e \"Sweaters\"\n        \"slug\" =\u003e \"sweaters\"\n        \"child\" =\u003e array:2 [\n          0 =\u003e array:5 [\n            \"id\" =\u003e 5\n            \"name\" =\u003e \"Red Sweaters\"\n            \"slug\" =\u003e \"red-sweaters\"\n            \"child\" =\u003e []\n            \"parent_id\" =\u003e 4\n          ]\n          1 =\u003e array:5 [\n            \"id\" =\u003e 6\n            \"name\" =\u003e \"Blue Sweaters\"\n            \"slug\" =\u003e \"blue-sweaters\"\n            \"child\" =\u003e []\n            \"parent_id\" =\u003e 4\n          ]\n        ]\n        \"parent_id\" =\u003e 0\n    ]\n]\n```\nFor html tree output:\n\n```php\n\u003c?php\n\nCategory::renderAsHtml();\n```\n\nOutput:\n\n```html\n\n\u003cul\u003e\n    \u003cli\u003e\u003ca href=\"\"\u003eT-shirts\n        \u003cul\u003e\n            \u003cli\u003e\u003ca href=\"red-t-shirt\"\u003eRed T-shirt\u003c/a\u003e\u003c/li\u003e\n            \u003cli\u003e\u003ca href=\"black-t-shirts\"\u003eBlack T-shirts\u003c/a\u003e\u003c/li\u003e\n        \u003c/ul\u003e\n    \u003c/li\u003e\n\n    \u003cli\u003e\u003ca href=\"\"\u003eSweaters\n        \u003cul\u003e\n            \u003cli\u003e\u003ca href=\"red-sweaters\"\u003eRed Sweaters\u003c/a\u003e\u003c/li\u003e\n            \u003cli\u003e\u003ca href=\"blue-sweaters\"\u003eBlue Sweaters\u003c/a\u003e\u003c/li\u003e\n        \u003c/ul\u003e\n    \u003c/li\u003e\n\u003c/ul\u003e\n```\n\nFor dropdown output:\n\n```php\n\u003c?php\n\nCategory::attr(['name' =\u003e 'categories'])\n    -\u003eselected(2)\n    -\u003erenderAsDropdown();\n```\n\nOutput:\n\n```html\n\u003cselect name=\"categories\"\u003e\n    \u003coption value=\"1\"\u003eT-shirts\u003c/option\u003e\n    \u003coption value=\"2\" selected=\"selected\"\u003e  Red T-shirts\u003c/option\u003e\n    \u003coption value=\"3\"\u003e  Black T-shirts\u003c/option\u003e\n\n    \u003coption value=\"4\"\u003eSweaters\u003c/option\u003e\n    \u003coption value=\"5\"\u003e  Red Sweaters\u003c/option\u003e\n    \u003coption value=\"6\"\u003e  Blue Sweaters\u003c/option\u003e\n\u003c/select\u003e\n```\n\nSelected for multiple list box:\n```php\n-\u003eselected([1,2,3])\n```\n\nOutput methods\n---\n\nname                  | Parameter |output    \n----------------------| --------- | -------\nrenderAsArray()       | none      | array   \nrenderAsJson()        | none      | json    \nrenderAsHtml()        | none      | html    \nrenderAsDropdown()    | none      | dropdown\nrenderAsMultiple()    | none      | Listbox\n\nUsable methods with output methods\n---\n\nrenderAsArray()\n---\nname                  | paremeter| description                      \n----------------------| -------- | --------------------------------\n[parent()](#parent)   | int      | Get childs of the defined parent\n\nrenderAsJson()\n---\nname                  | paremeter| description                      \n----------------------| -------- | --------------------------------\n[parent()](#parent)   | int      | Get childs of the defined parent\n\nrenderAsHtml()\n---\nname                  | paremeter         | description                      \n----------------------| ----------------- | --------------------------------\n[parent()](#parent)   | int               | Get childs of the defined parent\n[active()](#active)   | callback/array/int| Selected item(s) for html output\n[ulAttr()](#ulAttr)   | array/string      | Add attribute to parent ul element\n[firstUlAttr()](#firstUlAttr)   | array/string      | Add attribute to parent ul element\n[route()](#route)     | callback/array    | Generate url by route name\n[customUrl()](#custom-url)     | string    | Generate custom url \n\nrenderAsDropdown()/renderAsMultiple()\n---\nname                  | paremeter         | description                      \n----------------------| ----------------- | --------------------------------\n[parent()](#parent)   | int               | Get childs of the defined parent\n[selected()](#selected)| callback/array/int| Selected item(s) for dropdown  \n[attr()](#attr)        | array             | Dropdown/listbox attributes\n\n\n\nparent()\n---\nGet childs of the defined parent.\n\n```php\n\u003c?php\n\nCategory::parent(2)-\u003erenderAsArray();\n```\n\u003e**Note:** This methods usable all with output methods\n\nactive()\n---\nSelected item(s) for html output.\n\nExample 1:\n```php\n\u003c?php\n\nMenu::active('t-shirts')-\u003erenderAsHtml();\n```\n\nExample 2:\n```php\n\u003c?php\n\nMenu::active('t-shirts', 'black-t-shirts')-\u003erenderAsHtml();\n```\n\nExample 3:\n```php\n\u003c?php\n\nMenu::active(['t-shirts', 'black-t-shirts'])-\u003erenderAsHtml();\n```\n\nExample 4:\n```php\n\u003c?php\n\nMenu::active(function($li, $href, $label) {\n\n    $li-\u003eaddAttr('class', 'active')-\u003eaddAttr('data-label', $label);\n\n})-\u003erenderAsHtml();\n```\n\nExample 5:\n```php\n\u003c?php\n\nMenu::active(function($li, $href, $label) {\n\n    $li-\u003eaddAttr(['class' =\u003e 'active', 'data-label' =\u003e $label]);\n\n})-\u003erenderAsHtml();\n```\n\nfirstUlAttr()\n---\nAdd attribute to first ul element\n\nExample 1:\n```php\n\u003c?php\n\nMenu::firstUlAttr('class', 'first-ul')-\u003erenderAsHtml();\n```\n\nExample 2:\n```php\n\u003c?php\n\nMenu::firstUlAttr(['class' =\u003e 'first-ul'])-\u003erenderAsHtml();\n```\n\nulAttr()\n---\nAdd attribute to parent ul element\n\nExample 1:\n```php\n\u003c?php\n\nMenu::ulAttr('class', 'nav-bar')-\u003erenderAsHtml();\n```\n\nExample 2:\n```php\n\u003c?php\n\nMenu::ulAttr(['t-shirts' =\u003e 'black-t-shirts'])-\u003erenderAsHtml();\n```\n\nExample 3:\n```php\n\u003c?php\n\nMenu::ulAttr(function($ul, $parent_id) {\n\n    if($parent_id == 10) {\n        $ul-\u003eulAttr('class', 'nav-bar');\n    }\n\n})-\u003erenderAsHtml();\n```\n\nroute()\n---\n\nGenerate url by route name\n\nExample 1:\n```php\n\u003c?php\n\nMenu::route(['product' =\u003e 'slug'])-\u003erenderAsHtml();\n\n```\n\u003e**Note:** **product** refer to route name and **slug** refer to paremeter name.\n\n```php\n\u003c?php\n\nRoute::get('product/{slug}', 'ProductController@show');\n```\n\nExample 2:\n```php\n\u003c?php\n\nMenu::route(function($href, $label, $parent) {\n\n    return \\URL::to($href);\n\n})-\u003erenderAsHtml();\n```\n\ncustomUrl()\n---\nGenerate custom url with slug\n\n\nExample 1:\n```php\n\u003c?php\n\nMenu::customUrl('product/detail/{slug}')-\u003erenderAsHtml();\n```\n\nExample 1:\n```php\n\u003c?php\n\nMenu::customUrl('product/{slug}/detail')-\u003erenderAsHtml();\n```\n\n\u003e**Note:** **slug** keyword belongs to html \u003e href in config file.\n\nselected()\n---\nSelected item(s) for dropdown.\n\nExample 1:\n```php\n\u003c?php\n\nCategory::selected(1)-\u003erenderAsDropdown();\n```\n\nExample 2:\n```php\n\u003c?php\n\nCategory::selected(1,5)-\u003erenderAsMultiple();\n```\n\nExample 3:\n```php\n\u003c?php\n\nCategory::selected([1,3])-\u003erenderAsMultiple();\n```\n\nExample 4:\n```php\n\u003c?php\n\nCategory::selected(function($option, $value, $label) {\n\n    $option-\u003eaddAttr('selected', 'true');\n    $option-\u003eaddAttr(['data-item' =\u003e $label]);\n\n})-\u003erenderAsMultiple();\n```\n\nattr()\n---\nDropdown/listbox attributes.\n\n```php\n\u003c?php\n\nCategory::attr(['name' =\u003e 'categories', 'class' =\u003e 'red'])-\u003erenderAsDropdown();\n```\n\nConfiguration\n---\n\nThe above examples were performed with default settings.\nConfig variables in **config/nestable.php** file.\n\nname                  | type    | description                      \n----------------------| ------- | --------------------------------\nparent                | string  | Parent category column name      \nprimary_key           | string  | Table primary key                \ngenerate_url          | boolean | Generate the url for html output\nchildNode             | string  | Child node name                  \n[body](#body)         | array   | Array output (default)           \n[html](#html)         | array   | Html output columns              \n[dropdown](#dropdown) | array   | Dropdown/Listbox output          \n\nbody\n---\n\nThe body variable should be an array and absolutely customizable.\n\nExample:\n```php\n\u003c?php\n\n'body' =\u003e [\n    'id',\n    'category_name',\n    'category_slug'\n]\n```\n\nhtml\n----\n\nConfiguration for html output.\n\nname         | description       \n-------------| -----------------\nlabel        | Label column name\nhref         | Url column name   \n\nExample:\n```php\n\u003c?php\n\n'html' =\u003e [\n    'label' =\u003e 'name',\n    'href'  =\u003e 'slug',\n]\n```\n\ndropdown\n----\n\nConfiguration for dropdown/listbox output.\n\nname   | description         \n------- | -------------------\nprefix | Label prefix        \nlabel  | Label column name   \nvalue  | Value column name   \n\nExample:\n\n```php\n\u003c?php\n\n'dropdown' =\u003e [\n    'prefix' =\u003e '-',\n    'label'  =\u003e 'name',\n    'value'  =\u003e 'id'\n]\n```\n\nUsing Independent Models\n---\n\nInclude the Nestable facade.\n\n```php\n\u003c?php\n\nuse Nestable;\n\n$result = Nestable::make([\n    [\n        'id' =\u003e 1,\n        'parent_id' =\u003e 0,\n        'name' =\u003e 'T-shirts',\n        'slug' =\u003e 't-shirts'\n    ],\n    [\n        'id' =\u003e 2,\n        'parent_id' =\u003e 1,\n        'name' =\u003e 'Red T-shirts',\n        'slug' =\u003e 'red-t-shirts'\n    ],\n    [\n        'id' =\u003e 3,\n        'parent_id' =\u003e 1,\n        'name' =\u003e 'Black T-shirts',\n        'slug' =\u003e 'black-t-shirts'\n    ]\n    // and more...\n]);\n```\n\nFor array output:\n```php\n$result-\u003erenderAsArray();\n```\n\nValidators\n---\nIt controls the structure of the data. They also made the rendering process with a second parameter control after they.\n\nname    | Parameters\n------- | -----------\nisValidForArray | boolean\nisValidForJson  | boolean\nisValidForHtml  | boolean\nisValidForDropdown| boolean\nisValidForMultiple | boolean\n\n**Example 1:**\n```php\n\u003c?php\n\nMenu::make($categories)-\u003eisValidForHtml();\n\n// return true or false\n```\n\n**Example 2:**\n```php\n\u003c?php\n\nMenu::make($categories)-\u003eisValidForHtml(true);\n\n// return html string if data valid\n```\n\nMacros\n---\n\n```php\n\u003c?php\n\nNestable::macro('helloWorld', function($nest, $categories) {\n\n    return $nest-\u003emake($categories)-\u003eactive('sweater')-\u003eroute(['tests' =\u003e 'slug'])-\u003erenderAsHtml();\n\n});\n```\n\nCall the above macro:\n\n```php\n\u003c?php\n\n$categories = [\n\n    [\n        'id'        =\u003e 1,\n        'parent_id' =\u003e 0,\n        'name'      =\u003e 'T-shirt',\n        'slug'      =\u003e 'T-shirt'\n    ],\n    [\n        'id'        =\u003e 2,\n        'parent_id' =\u003e 0,\n        'name'      =\u003e 'Sweater',\n        'slug'      =\u003e 'sweater'\n    ]\n\n];\n\nNestable::helloWorld($categories);\n```\n\nHelper\n---\n\n```php\n\u003c?php\n\nnestable($data)-\u003erenderAsHtml();\n```\n\n```php\n\u003c?php\n\nnestable()-\u003emake($data)-\u003erenderAsHtml();\n```\n\n```php\n\u003c?php\n\nnestable()-\u003emacro('helloWorld', function() {\n    return 'Hello Laravel';\n});\n\n// run\nnestable()-\u003ehelloWorld();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcblogdev%2Flaravel-nestable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcblogdev%2Flaravel-nestable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcblogdev%2Flaravel-nestable/lists"}