{"id":28480790,"url":"https://github.com/typicms/nestablecollection","last_synced_at":"2025-07-03T19:32:53.508Z","repository":{"id":27491657,"uuid":"30971812","full_name":"TypiCMS/NestableCollection","owner":"TypiCMS","description":"A Laravel Package that extends Eloquent\\Collection to handle nested items following adjacency list model.","archived":false,"fork":false,"pushed_at":"2025-03-03T15:44:57.000Z","size":54,"stargazers_count":87,"open_issues_count":2,"forks_count":27,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-06-07T19:07:23.285Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/TypiCMS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"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":"typicms"}},"created_at":"2015-02-18T15:37:50.000Z","updated_at":"2025-03-03T15:44:32.000Z","dependencies_parsed_at":"2024-03-12T17:51:59.185Z","dependency_job_id":null,"html_url":"https://github.com/TypiCMS/NestableCollection","commit_stats":{"total_commits":89,"total_committers":10,"mean_commits":8.9,"dds":0.2471910112359551,"last_synced_commit":"3b690da6383cd3c73b4f02725e804272f41738b2"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"purl":"pkg:github/TypiCMS/NestableCollection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypiCMS%2FNestableCollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypiCMS%2FNestableCollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypiCMS%2FNestableCollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypiCMS%2FNestableCollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TypiCMS","download_url":"https://codeload.github.com/TypiCMS/NestableCollection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TypiCMS%2FNestableCollection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263388660,"owners_count":23459244,"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":"2025-06-07T19:07:22.149Z","updated_at":"2025-07-03T19:32:53.116Z","avatar_url":"https://github.com/TypiCMS.png","language":"PHP","funding_links":["https://github.com/sponsors/typicms"],"categories":["PHP"],"sub_categories":[],"readme":"# NestableCollection\n\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n\nA Laravel Package that extends collections to handle nested items following adjacency list model.\n\n## Installation\n\nRun `composer require typicms/nestablecollection`\n\n## Usage\n\nThe model must have a **parent_id** attributes :\n\n```php\nprotected $fillable = [\n    'parent_id',\n    // …\n];\n```\n\nand must use the following trait:\n\n```php\nuse TypiCMS\\NestableTrait;\n```\n\nNow each time you get a collection of that model, it will be an instance of **TypiCMS\\NestableCollection** in place of **Illuminate\\Database\\Eloquent\\Collection**.\n\nIf you want a tree of models, simply call the nest method on a collection ordered by parent_id asc :\n\n```php\nModel::orderBy('parent_id')-\u003eget()-\u003enest();\n```\n\nOf course you will probably want a position column as well. So you will have to order first by parent_id asc and then by position asc.\n\n## Change the name of subcollections\n\nBy default, the name of the subcollections is **items**, but you can change it by calling the `childrenName($name)` method :\nFor example if you want your subcollections being named **children**:\n\n```php\n$collection-\u003echildrenName('children')-\u003enest();\n```\n\n## Indented and flattened list\n\n`listsFlattened()` method generate the tree as a flattened list with id as keys and title as values, perfect for select/option, for example :\n\n```php\n[\n    '22' =\u003e 'Item 1 Title',\n    '10' =\u003e '    Child 1 Title',\n    '17' =\u003e '    Child 2 Title',\n    '14' =\u003e 'Item 2 Title',\n]\n```\n\nTo use it, first call the `nest()` method, followed by the `listsFlattened()` method:\n\n```php\nModel::orderBy('parent_id')-\u003eget()-\u003enest()-\u003elistsFlattened();\n```\n\nBy default it will look for a `title` column. You can send a custom column name as first parameter:\n\n```php\nModel::orderBy('parent_id')-\u003eget()-\u003enest()-\u003elistsFlattened('name');\n```\n\nFour spaces are used to indent by default, to use your own use the `setIndent()` method, followed by the `listsFlattened()` method:\n\n```php\nModel::orderBy('parent_id')-\u003eget()-\u003enest()-\u003esetIndent('\u003e ')-\u003elistsFlattened();\n```\n\nResults:\n\n```php\n[\n    '22' =\u003e 'Item 1 Title',\n    '10' =\u003e '\u003e Child 1 Title',\n    '17' =\u003e '\u003e Child 2 Title',\n    '14' =\u003e 'Item 2 Title',\n]\n```\n\n## Nesting a subtree\n\nThis package remove items that have missing ancestor, this doesn’t allow you to nest a branch of a tree.\nTo avoid this, you can use the `noCleaning()` method:\n\n```php\nModel::orderBy('parent_id')-\u003eget()-\u003enoCleaning()-\u003enest();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypicms%2Fnestablecollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypicms%2Fnestablecollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypicms%2Fnestablecollection/lists"}