{"id":37012665,"url":"https://github.com/denis-kisel/nested-categories","last_synced_at":"2026-01-14T01:13:05.071Z","repository":{"id":57698382,"uuid":"503649930","full_name":"denis-kisel/nested-categories","owner":"denis-kisel","description":"Nested categories","archived":false,"fork":false,"pushed_at":"2022-10-04T10:38:06.000Z","size":50,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-27T14:50:34.602Z","etag":null,"topics":["categories","category","child","fast","items","laravel","nested","nested-structures","php","quick","structure","tree","tree-structure"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/denis-kisel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-15T06:53:11.000Z","updated_at":"2023-09-08T18:35:24.000Z","dependencies_parsed_at":"2022-08-25T11:20:58.263Z","dependency_job_id":null,"html_url":"https://github.com/denis-kisel/nested-categories","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/denis-kisel/nested-categories","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denis-kisel%2Fnested-categories","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denis-kisel%2Fnested-categories/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denis-kisel%2Fnested-categories/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denis-kisel%2Fnested-categories/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/denis-kisel","download_url":"https://codeload.github.com/denis-kisel/nested-categories/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denis-kisel%2Fnested-categories/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28407658,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T00:40:43.272Z","status":"ssl_error","status_checked_at":"2026-01-14T00:40:42.636Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["categories","category","child","fast","items","laravel","nested","nested-structures","php","quick","structure","tree","tree-structure"],"created_at":"2026-01-14T01:13:04.228Z","updated_at":"2026-01-14T01:13:05.056Z","avatar_url":"https://github.com/denis-kisel.png","language":"PHP","readme":"# nested-categories\n\nThis library provide optimize common usage categories functional, such as: `category tree`, `breadcrumbs`, `child category items` by one sql query\n\n### Support for\n`mysql5.7.22+`\n`laravel`\n`php8.0+`\n\n### Install package\n\nInstall via composer\n```\ncomposer require denis-kisel/nested-categories\n```\n\n\n\n### Install or upgrade Category table\n\nIf you have not yet category table, install it:\n```\n# Created table 'categories'\nphp artisan nested-category:install \n\n#Or specify table name\nphp artisan nested-category:install --table-name=categories\n```\n\n\nIf you have category table already, just upgrade it:  \n**Suppose what your table has fields: `id`, `parent_id`**\n```\n#Specify model to upgrade\nphp artisan nested-category:upgrade App\\\\Models\\\\Category\n```\n\n\n### \u003ca id=\"configure\"\u003e\u003c/a\u003eConfigure\nAdd trait `NestableCategory` to category model\n```php\nuse DenisKisel\\NestedCategory\\NestableCategory;\n\nclass Category extends Model\n{\n    use NestableCategory;\n    ....\n}\n```\n\nAdd `path` to fillable or set guarded\n```php\nclass Category extends Model\n{\n    ....\n    protected $fillable = ['path'];\n    ....\n}\n\n#OR\nclass Category extends Model\n{\n    ....\n    protected $guarded = [];\n    ....\n}\n```\n\n**[Optional]**  \nAdd trait `AutoRebuildNested` to category model for auto rebuild category structure after events: `created`, `updated`, `deleted`. \nBUT ITS DONT AUTO REBUILD AFTER BATCH OPERATION(see [Rebuild Structure](#rebuild-structure)). \n\n\u003e Background use one query for rebuild all table\n\n```php\nuse DenisKisel\\NestedCategory\\NestableCategory;\nuse DenisKisel\\NestedCategory\\AutoRebuildNested;\n\nclass Category extends Model\n{\n    use NestableCategory, AutoRebuildNested;\n    ....\n}\n\n#The same\n$category-\u003esave();\n$category-\u003erebuild();\n\n$category-\u003eupdate();\n$category-\u003erebuild();\n\n$category-\u003edelete();\n$category-\u003erebuild();\n```\n\n## Usage\n### Tree As Array\n| id  | parent_id | name   | order |\n|-----|-----------|--------|-------|\n| 1   | NULL      | Parent | 0     |\n| 2   | 1         | Child1 | 0     |\n| 3   | 1         | Child2 | 1     |\n```php\n$result = Category::asArrayTree();\ndump($result)\n//Output:\n[\n    'id' =\u003e 1,\n    'parent_id' =\u003e NULL,\n    'name' =\u003e 'Parent',\n    'children' =\u003e [\n        [\n            'id' =\u003e 2,\n            'parent_id' =\u003e 1,\n            'name' =\u003e 'Child1',\n            'children' =\u003e []\n        ],\n        [\n            'id' =\u003e 3,\n            'parent_id' =\u003e 1,\n            'name' =\u003e 'Child2',\n            'children' =\u003e []\n        ]\n    ]      \n]\n\n# Specify needed fields\n$result = Category::asArrayTree(fields: ['name', 'order']);\ndump($result)\n//Output:\n[\n    'name' =\u003e 'Parent',\n    'order' =\u003e 0,\n    'children' =\u003e [\n        [\n            'name' =\u003e 'Child1',\n            'order' =\u003e 0,\n            'children' =\u003e []\n        ],\n        [\n            'name' =\u003e 'Child2',\n            'order' =\u003e 1,\n            'children' =\u003e []\n        ]\n    ]      \n]\n\n# Specify cache time(minutes or DateTimeInterface)\n$result = Category::asArrayTree(cacheTTL: 10);\n//Cached data\n\n$result = Category::asArrayTree(cacheTTL: 10);\n//Data from previous cache\n\n$result = Category::asArrayTree();\n//Data without cache\n\n\n# Get array of objects\n$result = Category::asArrayTree(associative: false);\ndump($result)\n//Output:\n[\n    {\n        name: 'Parent',\n        order: 0,\n        children: [{...}]\n    },\n....\n]\n```\n\n\n### Breadcrumbs\nBackend use one sql query for N nested categories\n```php\n$category = Category::find(2)\ndump($category-\u003ebreadcrumbs());\n\n//Output\nCollection {\n    array:2 [\n        Category {id: 1, ...},\n        Category {id: 2, ...},\n    ]   \n}\n```\n\n\n### Leafs/child category items(Nested Products, Posts, Podcasts, etc..)\nInput tables: `categories`(id, parent_id, name), `products`(id, category_id, name).  \nBackend use one sql query for nested leafs\n```\nParentCategory(id: 1)\n│   Product_1\n│   Product_2    \n│\n└───ChildCategory_1(id: 2)\n│   │   Product_3\n│   │   Product_4\n│   │\n│   └───ChildCategory_1_1(id: 3)\n│       │   Product_5\n│       │   Product_6\n│   \n└───ChildCategory_2(id: 4)\n    │   Product_7\n    │   Product_8\n```\n\n```\n#GetAllProducts\n$products = Category::find(1)-\u003eleafs(App\\Models\\Product::class)-\u003eget();\ndump($products-\u003ecount());\n//Output: 8\n```\n\n```php\n#In Models\\Category\n....\npublic nestedProducts() :Builder\n{\n    return $this-\u003eleafs(Product::class)\n}\n\npublic nestedPosts() :Builder\n{\n    return $this-\u003eleafs(Post::class)\n}\n....\n\n#Client Code\n$products = Category::find(1)-\u003enestedProducts()-\u003ewhere('name', 'like', '%some%')-\u003eget();\n$products = Category::first()-\u003enestedPosts()-\u003ecount();\n```\n\n### \u003ca id=\"rebuild-structure\"\u003e\u003c/a\u003eRebuild Structure\nAfter BATCH CRUD operations for rebuild categories structure, need to use `rebuild` method.\nOr you can use trait `AutoRebuildNested` after single operation(see more in [Configure](#configure))\n\u003e Background use one query for rebuild all table\n```php\n# Inserts\nCategory::insert([\n    ['id' =\u003e 1, 'parent_id' =\u003e null],\n    ['id' =\u003e 2, 'parent_id' =\u003e 1],\n    ['id' =\u003e 3, 'parent_id' =\u003e 2],\n    .....\n])\n\nCategory::rebuild();\n\n# Delete\nCategory::where('is_active', false)-\u003edelete();\nCategory::rebuild();\n```\n\n### Additional Commands\n```\n# Rebuild specify category\nphp artisan nested-category:rebuild App\\\\Models\\\\Category\n```\n\n\n### Testing\n```\ncd vendor/denis-kisel/nested-categories\nvendor/bin/phpunit test\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenis-kisel%2Fnested-categories","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdenis-kisel%2Fnested-categories","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenis-kisel%2Fnested-categories/lists"}