{"id":43078787,"url":"https://github.com/codrasil/closurable","last_synced_at":"2026-01-31T14:30:30.067Z","repository":{"id":56956266,"uuid":"198422038","full_name":"codrasil/closurable","owner":"codrasil","description":"Closure Table relational database implementation in PHP for any hierarchical data.","archived":false,"fork":false,"pushed_at":"2020-05-07T09:12:55.000Z","size":103,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-09T21:44:22.816Z","etag":null,"topics":["closure-table","hierarchy","menus","tree"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/codrasil/closurable","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/codrasil.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":"2019-07-23T12:02:46.000Z","updated_at":"2023-10-21T02:18:20.000Z","dependencies_parsed_at":"2022-08-21T08:50:40.621Z","dependency_job_id":null,"html_url":"https://github.com/codrasil/closurable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/codrasil/closurable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codrasil%2Fclosurable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codrasil%2Fclosurable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codrasil%2Fclosurable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codrasil%2Fclosurable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codrasil","download_url":"https://codeload.github.com/codrasil/closurable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codrasil%2Fclosurable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28945523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T14:26:55.697Z","status":"ssl_error","status_checked_at":"2026-01-31T14:26:52.545Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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":["closure-table","hierarchy","menus","tree"],"created_at":"2026-01-31T14:30:29.461Z","updated_at":"2026-01-31T14:30:30.059Z","avatar_url":"https://github.com/codrasil.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Closurable\n\n[![Latest Stable Version](https://poser.pugx.org/codrasil/closurable/v/stable)](https://packagist.org/packages/codrasil/closurable)\n[![Total Downloads](https://poser.pugx.org/codrasil/closurable/downloads)](https://packagist.org/packages/codrasil/closurable)\n[![Latest Unstable Version](https://poser.pugx.org/codrasil/closurable/v/unstable)](https://packagist.org/packages/codrasil/closurable)\n[![License](https://poser.pugx.org/codrasil/closurable/license)](https://packagist.org/packages/codrasil/closurable)\n\n---\n\n\u003cp align=\"center\"\u003e\u003cimg style=\"display:block;margin:2rem auto;text-align:center\" src=\"./docs/closurables.tree.svg\" width=\"600\"\u003e\u003c/center\u003e\u003c/p\u003e\nClosure Table relational database implementation in PHP for any hierarchical data.\n\n### Requirements\n\n* `PHP`: `^7.x`\n* `MySQL`: any version should be fine.\n\n---\n\n### Installation\n\nThis package is originally built for \u003ca href=\"https://github.com/laravel/laravel\"\u003eLaravel\u003c/a\u003e, but can also be used in any PHP project.\n\n\u003ca href=\"docs/integration.md\"\u003eRead more about integration in the docs.\u003c/a\u003e\n\n**via composer:**\n```bash\ncomposer require codrasil/closurable\n```\n\n\n### Publishing Configuration\nPass in the artisan command the package's service provider:\n\n```bash\nphp artisan vendor:publish --provider=\"Codrasil\\Closurable\\NestableServiceProvider\"\n```\n\n---\n\n### Setup\n\n#### Generating Migration File\nFirst, run the console command to generate a nested migration table.\n\nFormat:\n```bash\nmake:closurable [options] [--] \u003creference\u003e\n```\n\nExample:\n\n_We will be using the `users` model to generate a nested relationship of users._\n```bash\nphp artisan make:closurable users\n```\n\nThe command accepts an argument of `referenced table`. This will be the table to be \"closured\".\n\nIt will generate a table named `userstree` that comes pre-populated with the necessary columns.\n\nThe generated file should look something like below:\n```php\nSchema::create('userstree', function (Blueprint $table) {\n    $table-\u003eunsignedBigInteger('ancestor_id')-\u003eindex();\n    $table-\u003eunsignedBigInteger('descendant_id')-\u003eindex();\n    $table-\u003eunsignedBigInteger('depth')-\u003eindex()-\u003edefault(0);\n    $table-\u003eunsignedBigInteger('root')-\u003eindex()-\u003edefault(0);\n    $table-\u003eunique(['ancestor_id', 'descendant_id']);\n    $table-\u003eindex(['ancestor_id', 'descendant_id', 'depth']);\n    $table-\u003eindex(['descendant_id', 'depth']);\n    $table-\u003eindex(['depth', 'root']);\n    $table-\u003eforeign('ancestor_id')\n          -\u003ereferences('id')\n          -\u003eon('users')\n          -\u003eonDelete('cascade')\n          -\u003eonUpdate('cascade');\n    $table-\u003eforeign('descendant_id')\n          -\u003ereferences('id')\n          -\u003eon('users')\n          -\u003eonDelete('cascade')\n          -\u003eonUpdate('cascade');\n});\n```\n\n**Note** that you should generate the migration file of the **referenced table** yourself before running the command (in the above example, you should generate the migration for the `users` table yourself).\n\n**Note** to change the table name of the closure table, pass in an option `--table` or `--create`:\n```bash\nphp artisan make:closurable users --table=familytree\n```\n\nRun `php artisan make:closurable --help` for more information on configuring the command.\n\n#### Model usage\nNext, use either of the two options on the model to be closure nested.\n\n_Following our example above, the `User` model should either implement:_\n\n* the trait, `Closurable`:\n\n  ```\n  use Codrasil\\Closurable\\Closurable;\n\n  class User extends Authenticatable\n  {\n      use Closurable;\n  }\n  ```\n\n  or\n\n* via extending the abstract class, `Codrasil\\Closurable\\Model`, instead of the default Illuminate Model class:\n\n  ```\n  use Codrasil\\Closurable\\Model;\n\n  class User extends Model\n  {\n     // Of course, you will need to reimplement the Authenticatable traits\n     // to the User model if you ARE going to nest the User model.\n  }\n  ```\n\n\n---\n\n### Usage\n\n#### Saving a Branch Node\n\nLet's say we have the following data on our `users` table:\n\n| ID |   Name                  |\n| -- | ----------------------- |\n| 1  | Henry Walton Jones, Sr. |\n| 2  | Indiana Jones           |\n| 3  | Susie Jones             |\n| 4  | Henry Walton Jones III  |\n\nAnd we need the following relationship:\n\n\u003cp align=\"center\"\u003e\u003cimg style=\"display:block;margin:2rem auto;text-align:center\" src=\"./docs/closurables.sample2.svg\" width=\"600\"\u003e\u003c/center\u003e\u003c/p\u003e\n\nTo save the relationships described above, we need to use the `closurables()` from the User model to access the `attach(Model $model)` method.\n\n```php\n$parent = User::find(1); // Jones, Sr.\n$junior = User::find(2); // Indy\n$parent-\u003eclosurables()-\u003eattach($junior);\n\n...\n\n$child = User::find(3); // Susie\n$parent-\u003eclosurables()-\u003eattach($child);\n\n...\n$child = User::find(4); // Jones III\n$junior-\u003eclosurables()-\u003eattach($child);\n```\n\nThe relationship will be saved in the `familytree` table as:\n\n| ancestor_id | descendant_id | depth | root |\n| ----------: | ------------: | ----: | ---: |\n|           1 |             1 |     0 |    1 |\n|           1 |             2 |     1 |    0 |\n|           1 |             3 |     1 |    0 |\n|           1 |             4 |     2 |    0 |\n|           2 |             2 |     0 |    0 |\n|           2 |             4 |     1 |    0 |\n|           3 |             3 |     0 |    0 |\n|           4 |             4 |     0 |    0 |\n\nVisual representation:\n\n\u003cimg src=\"./docs/closurables.sample1.svg\" width=\"100%\"\u003e\n\n---\n\n##### Displaying Root Nodes\n\n* **Root**\n\n    To display resources without parents, use the `roots` scope:\n\n    ```php\n    $roots = MyModel::roots()-\u003eget();\n    ```\n\n---\n\n#### Querying for Adjacent Relations\n\nBy default, sorting is handled via the `sort` column found in the **reference table**.\nIf the `sort` column is unavailable, it will default to `id` or whatever `$this-\u003egetKeyName()` will return.\n\n* **Siblings**\n\n    To retrieve all siblings of a child, use `siblings()` method:\n\n    ```php\n    $child = MyModel::find(2);\n    $siblings = $child-\u003esiblings(); // or $child-\u003esiblings\n    ```\n\n    * A helper method `-\u003ehasSiblings()` for checking emptiness is available.\n    * An accessor method `getSiblingsAttribute` is available.\n\n    ---\n\n* **Next Sibling**\n\n    To display the next sibling in the `$user-\u003echildren`, use `next()` method:\n\n    ```php\n    $firstChild = MyModel::find(2);\n    $secondChild = $firstChild-\u003enext(); // or $firstChild-\u003enext\n    ```\n\n    * A helper method `-\u003ehasNext()` for checking nullness is available.\n    * An accessor method `getNextAttribute` is available.\n\n    ---\n\n* **Previous Sibling**\n\n    To display the previous sibling, use `previous()` method:\n\n    ```php\n    $secondChild = MyModel::find(3);\n    $firstChild = $secondChild-\u003eprevious(); // or $secondChild-\u003eprevious\n    ```\n\n    * A helper method `-\u003ehasPrevious()` for checking nullness is available.\n    * An accessor method `getPreviousAttribute` is available.\n\n---\n\n#### Querying for Lineal Relations\n\n* **Parent**\n\n    To retrieve the immediate parent of the child, use `parent()` method:\n\n    ```php\n    $child = MyModel::find(2);\n    $parent = $child-\u003eparent(); // or $child-\u003eparent\n    ```\n\n    * A helper method `-\u003ehasParent()` for checking nullness is available.\n    * An accessor method `getParentAttribute` is available.\n\n* **Children**\n\n    To display the children nodes of a specific resource, use the `children()` method:\n\n    ```php\n    $user = User::find(1);\n    ```\n\n    ```blade\n    {{-- in a blade file --}}\n\n    @foreach ($user-\u003echildren() as $child)\n      {{ $child-\u003ename }}\n    @endforeach\n\n    {{-- use @dd($user-\u003echildren) to see entire collection --}}\n    ```\n\n    * A helper method `-\u003ehasChildren()` for checking emptiness is available.\n    * An accessor method `getChildrenAttribute` is available.\n\n    ---\n\n* **Ancestors**\n\n    To retrieve all parents of the child (and the parent of the child's parent, and so on), use `ancestors()` method:\n\n    ```php\n    $child = MyModel::find(4);\n    $ancestors = $child-\u003eancestors();\n    // will output the parent([of the parent]*n) + the child.\n    // dd($ancestors) to inspect actual data.\n    ```\n\n    * A helper method `-\u003ehasAncestors()` for checking emptiness is available.\n    * An accessor method `getAncestorsAttribute` is available.\n\n    ---\n\n* **Descendants**\n\n    To retrieve all children of the parent (and the children of the parent's children, and so on), use `descendants()` method:\n\n    ```php\n    $parent = MyModel::find(2);\n    $descendants = $parent-\u003edescendants();\n    // will output the children([of the children]*n) + the parent.\n    // dd($descendants) to inspect actual data.\n    ```\n\n    * A helper method `-\u003ehasDescendants()` for checking emptiness is available.\n    * An accessor method `getDescendantsAttribute` is available.\n\nFor more use cases of `adjacent` and `lineal` relations, checkout \u003ca href=\"./docs/relations.md\"\u003eRelations\u003c/a\u003e section in the docs.\n\n---\n\n### Documentation \u0026 Examples\n\nTo learn more about the API, visit the \u003ca href=\"./docs\"\u003edocs\u003c/a\u003e folder.\n\nFor more example implementation, checkout \u003ca href=\"./docs/examples\"\u003edocs/examples\u003c/a\u003e folder. The examples page has a variety of use cases like \u003ca href=\"./docs/examples/Commenting System.md\"\u003eCommenting System\u003c/a\u003e, \u003ca href=\"./docs/examples/Taxonomic Ranking.md\"\u003eTaxonomic Ranking of the Animal Kingdom\u003c/a\u003e, \u003ca href=\"./docs/examples/Family Trees.md\"\u003eFamily Trees\u003c/a\u003e, and more.\n\n### License\nThe library is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodrasil%2Fclosurable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodrasil%2Fclosurable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodrasil%2Fclosurable/lists"}