{"id":28748871,"url":"https://github.com/adrianskierniewski/eloquent-tree","last_synced_at":"2025-07-23T00:34:26.927Z","repository":{"id":12579904,"uuid":"15250563","full_name":"AdrianSkierniewski/eloquent-tree","owner":"AdrianSkierniewski","description":"Eloquent Tree is a tree model for Laravel Eloquent ORM.","archived":false,"fork":false,"pushed_at":"2018-09-15T06:00:53.000Z","size":77,"stargazers_count":133,"open_issues_count":2,"forks_count":21,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-07-20T08:52:47.556Z","etag":null,"topics":["composer","laravel","laravel-package","php","tree-model"],"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/AdrianSkierniewski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-17T09:41:43.000Z","updated_at":"2025-03-30T20:14:55.000Z","dependencies_parsed_at":"2022-09-13T14:52:32.996Z","dependency_job_id":null,"html_url":"https://github.com/AdrianSkierniewski/eloquent-tree","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/AdrianSkierniewski/eloquent-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdrianSkierniewski%2Feloquent-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdrianSkierniewski%2Feloquent-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdrianSkierniewski%2Feloquent-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdrianSkierniewski%2Feloquent-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AdrianSkierniewski","download_url":"https://codeload.github.com/AdrianSkierniewski/eloquent-tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdrianSkierniewski%2Feloquent-tree/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266597014,"owners_count":23953894,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"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":["composer","laravel","laravel-package","php","tree-model"],"created_at":"2025-06-16T19:13:11.212Z","updated_at":"2025-07-23T00:34:26.907Z","avatar_url":"https://github.com/AdrianSkierniewski.png","language":"PHP","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=6YKG4RZRQF3GS"],"categories":[],"sub_categories":[],"readme":"eloquent-tree [![Latest Stable Version](https://poser.pugx.org/gzero/eloquent-tree/v/stable.png)](https://packagist.org/packages/gzero/eloquent-tree) [![Total Downloads](https://poser.pugx.org/gzero/eloquent-tree/downloads.png)](https://packagist.org/packages/gzero/eloquent-tree) [![Build Status](https://travis-ci.org/AdrianSkierniewski/eloquent-tree.png)](https://travis-ci.org/AdrianSkierniewski/eloquent-tree)\n=============\n\nEloquent Tree is a tree model for Laravel Eloquent ORM.\n\n## Table of Contents\n\n- [Features](#features)\n- [Installation](#installation)\n- [Migration](#migration)\n- [Example usage](#example-usage)\n- [Events](#events)\n- [Support](#support)\n\n##Features\n\n* Creating root, children and sibling nodes\n* Getting children\n* Getting descendants\n* Getting ancestor\n* Moving sub-tree\n* Building tree on PHP side\n\n\n## Installation\n\n**Version 1.0 is not compatible with 0.***\n\n**Version 2.0 - Laravel 5 support**\n\n**Version 2.1 - Laravel 5.1 support**\n\n**Version 3.0 - Laravel 5.3 support**\n\nBegin by installing this package through Composer. Edit your project's composer.json file to require gzero/eloquent-tree.\n```json\n\"require\": {\n    \"laravel/framework\": \"5.3.*\",\n    \"gzero/eloquent-tree\": \"v3.0.*\"\n},\n\"minimum-stability\" : \"stable\"\n```\nNext, update Composer from the Terminal:\n```\ncomposer update\n```\nThat's all now you can extend \\Gzero\\EloquentTree\\Model\\Tree in your project\n\n## Migration\nSimply migration with all required columns that you could extend by adding new fields\n```php\nSchema::create(\n    'trees',\n    function (Blueprint $table) {\n        $table-\u003eincrements('id');\n        $table-\u003estring('path', 255)-\u003enullable();\n        $table-\u003einteger('parent_id')-\u003eunsigned()-\u003enullable();\n        $table-\u003einteger('level')-\u003edefault(0);\n        $table-\u003etimestamps();\n        $table-\u003eindex(array('path', 'parent_id', 'level'));\n        $table-\u003eforeign('parent_id')-\u003ereferences('id')-\u003eon('contents')-\u003eonDelete('CASCADE');\n    }\n);\n```\n\n## Example usage\n\n- [Inserting and Updating new nodes](#inserting-and-updating-new-nodes)\n- [Getting tree nodes](#getting-tree-nodes)\n- [Finding Leaf nodes](#getting-leaf-nodes)\n- [Map from array](#map-from-array)\n- [Rendering tree](#rendering-tree)\n\n### Inserting and updating new nodes\n\n```php\n$root       = new Tree(); // New root\n$root-\u003esetAsRoot();\n$child      = with(new Tree())-\u003esetChildOf($root); // New child\n$sibling    = new Tree();\n$sibling-\u003esetSiblingOf($child); // New sibling\n```\n\n### Getting tree nodes\n\nLeaf - returning root node\n```php\n$leaf-\u003efindRoot();\n```\n\nChildren - returning flat collection of children. You can use Eloquent query builder.\n```php\n$collection = $root-\u003echildren()-\u003eget();\n$collection2 = $root-\u003echildren()-\u003ewhere('url', '=', 'slug')-\u003eget();\n```\nAncestors - returning flat collection of ancestors, first is root, last is current node. You can use Eloquent query builder.\n            Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where\n```php\n$collection = $node-\u003efindAncestors()-\u003eget();\n$collection2 = $node-\u003efindAncestors()-\u003ewhere('url', '=', 'slug')-\u003eget();\n```\n\nDescendants - returning flat collection of descendants, first is current node, last is leafs. You can use Eloquent query builder.\n            Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where\n```php\n$collection = $node-\u003efindDescendants()-\u003eget();\n$collection2 = $node-\u003efindDescendants()-\u003ewhere('url', '=', 'slug')-\u003eget();\n```\n\nBuilding tree structure on PHP side - if some nodes will be missing, these branches will not be built\n```php\n$treeRoot = $root-\u003ebuildTree($root-\u003efindDescendants()-\u003eget())\n```\n\n### Getting leaf nodes\n```php\nTree::getLeaves();\n```\n\n### Map from array\n\nThree new roots, first with descendants\n```php\n Tree::mapArray(\n            array(\n                array(\n                    'children' =\u003e array(\n                        array(\n                            'children' =\u003e array(\n                                array(\n                                    'children' =\u003e array(\n                                        array(\n                                            'children' =\u003e array()\n                                        ),\n                                        array(\n                                            'children' =\u003e array()\n                                        )\n                                    )\n                                ),\n                                array(\n                                    'children' =\u003e array()\n                                )\n                            )\n                        ),\n                        array(\n                            'children' =\u003e array()\n                        )\n                    )\n                ),\n                array(\n                    'children' =\u003e array()\n                ),\n                array(\n                    'children' =\u003e array()\n                )\n            )\n );\n```\n\n### Rendering tree\n\nYou can render tree built by the function buildTree\n```php\n $html = $root-\u003erender(\n        'ul',\n        function ($node) {\n            return '\u003cli\u003e' . $node-\u003etitle . '{sub-tree}\u003c/li\u003e';\n        },\n        TRUE\n        );\n echo $html;\n```\n\n## Events\n\nAll tree models have additional events:\n* updatingParent\n* updatedParent\n* updatedDescendants\n\nYou can use them for example to update additional tables\n\n## Support\n\nIf you enjoy my work, please consider making a small donation, so I can continue to maintain and create new software to help\nother users.\n\n[![PayPal](https://www.paypalobjects.com/en_US/GB/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=6YKG4RZRQF3GS)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrianskierniewski%2Feloquent-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadrianskierniewski%2Feloquent-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrianskierniewski%2Feloquent-tree/lists"}