lazychaser/laravel-nestedset: Nested Set Trees for Laravel Models
Effective tree structures in Laravel 4-8
At a glance
- What is it?
- A Laravel package that stores hierarchical data as nested sets, trading cheap reads for expensive writes. It suits menus and category trees that change rarely, and it is the wrong tool for feeds and comment threads.
- Who is it for?
- Adopt lazychaser/laravel-nestedset for category trees, multi-depth menus and any hierarchy that is read far more often than it is restructured, and for Laravel versions the README maps to a release. Do not adopt it for comment threads, activity feeds or anything where users insert nodes continuously, because the nested set model renumbers on writes.
- Can I use it commercially?
- Not without permission. GitHub finds no licence file in the repository, and without a licence all rights are reserved by default: you may read the code but not reuse it. Check the README, or ask the authors, before using it.
- Is it still maintained?
- Activity is slowing. The repository last received commits 7 months ago.
- What is it written in?
- Mainly PHP, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.
Star history
- lazychaser/laravel-nestedset3,803 ★ · +0
| Repository | 2026-09-24 | 2026-11-15 |
|---|---|---|
| lazychaser/laravel-nestedset | 3.8k | 3.8k |
0 Stars in the last 52 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The hierarchy problem nested sets solve in Laravel
A parent_id column answers one question well: who is my parent. It answers the expensive questions badly. Give me every descendant of this category, or the full path from the root to this node, and you are either writing a recursive query or looping in PHP until the tree bottoms out. The README frames the alternative plainly: nested sets number the nodes according to a tree traversal, visiting each node twice and storing two numbers per row. Hierarchy membership then becomes a comparison between those numbers, so a descendant query is a range condition rather than a recursion. The README states the trade directly: querying becomes inexpensive, updating requires renumbering and is therefore expensive. The package targets the case where that trade pays off. Its own applications section names multi-depth menus and shop categories. Those are trees an editor rearranges occasionally and every request reads.
How the nested set columns and relationships work
Each node carries left and right boundary values that describe the interval it covers. A node's descendants are the rows whose boundaries fall inside that interval, and ancestors are the rows whose intervals contain it. The README lists four relationships that are fully functional and can be eagerly loaded: a node belongs to a parent, has many children, has many ancestors, and has many descendants. Eager loading matters here because it keeps the path and subtree queries from turning into one query per level. Insertion is the part that surprises people. The README warns that moving and inserting nodes includes several database queries, that transactions are highly recommended, and that as of v4.2.0 a transaction is not started automatically. Structural manipulations are also deferred until save is called on the model, and some methods call save implicitly. A successful save does not prove the node moved, which is why the README points at hasMoved for code that depends on the new position.
Installing kalnoy/nestedset and building a first category tree
The package installs through Composer under the name kalnoy/nestedset, which is how the README's badges identify it. The README's installation and requirements sections cover the setup steps. After installation you add the trait to a model and create the table with the nested set columns. The exact migration helper is not shown in the README excerpt, so check the installation section before generating the schema. Composer installs the package with its Packagist name:
composer require kalnoy/nestedsetThe README's examples assume a model such as Category that uses the package's NodeTrait, so that trait has to be added before any of the node methods below will work.
Once the trait is on the model, creating a node without a parent makes it a root, and the README shows that a plain create call saves the node at the end of the tree.
Category::create($attributes); // Saved as rootThe README also documents building a whole tree in one call by nesting a children key inside the attributes array. The static create method detects that key and creates the child nodes recursively, and afterwards the parent's children relation holds the nodes it created.
$node = Category::create([
'name' => 'Foo',
'children' => [
[
'name' => 'Bar',
'children' => [
[ 'name' => 'Baz' ],
],
],
],
]);For attaching an existing node, the README lists several equivalent forms: appendToNode followed by save, the parent's appendNode, creating through the children relation, associating the parent relation, assigning parent_id directly, or passing the parent as a second argument to create. Prepending has two documented forms, prependToNode with save and the parent's prependNode.
rebuildTree and the consistency tooling
The feature that separates this package from a thin parent_id wrapper is rebuildTree. You hand it an array of nodes, each optionally carrying an id and a children array, and it saves existing nodes, creates nodes without a primary key, and throws ModelNotFoundException when an id does not exist. The second argument controls whether nodes absent from the data are deleted, and by default they are not. The README presents this as the way to mass-change a tree's structure. Since 4.2.8 there is also rebuildSubtree, which confines the rebuild to the descendants of a given root. The README's table of contents also promises a consistency checking and fixing section. That is the practical safety net for nested sets: a renumbering step that half-completes leaves boundaries that overlap or leave gaps, and the tree silently returns wrong subtrees. Treat that section as required reading rather than optional.
Where nested sets are the wrong structure
The failure mode is write volume. Every structural change renumbers the boundaries of surrounding nodes, so an insert near the root touches a large slice of the table. A comment thread where replies arrive constantly, or a feed where new branches appear per second, turns that renumbering into a queue of lock contention. The README itself scopes the package to trees that are updated rarely. There is a second, quieter cost: nothing in the schema stops a buggy code path from writing boundaries directly, and once they are wrong the queries return plausible but incorrect subtrees rather than failing loudly. That is why the consistency section exists. A third constraint is version coupling. The README maps Laravel releases to package tags, Laravel 13 since v7.0.0, Laravel 12 since v6.0.7, Laravel 11.0 since v6.0.4, Laravel 10.0 since v6.0.2, Laravel 9.0 since v6.0.1, Laravel 8.0 since v6.0.0, and older Laravel lines to v5, v4, v3 and v2. Installing a tag below the line for your framework is not a supported combination.
Adjacency lists and other Laravel tree approaches
The direct alternative in the same ecosystem is an adjacency list implementation such as Staudenmeir/laravel-adjacency-list, which is one of the related searches people use. The difference is the storage model, not the API surface. An adjacency list keeps a parent reference per row and resolves depth with recursive CTEs, so writes are a single row update and reads pay for the recursion. Nested sets invert that: reads are range comparisons and writes renumber. If your tree is edited in an admin panel a few times a day, the nested set numbers are the cheaper side of the trade. If nodes are inserted by end users continuously, the adjacency list wins on write cost and you accept the recursive read. Other packages in the same space appear in the related searches, including Efureev laravel trees, Rinvex laravel categories and a Spatie package, but the README gives no comparison against any of them, so evaluate those on their own documentation rather than on this package's.
Maintenance, upgrades and licence questions
The repository is not archived and the last push was on 2026-04-11, which is recent enough to treat the project as maintained. The release history is uneven, though. v6.0.6 shipped on 2025-05-26 and v6.0.5 on 2025-03-20, while v4.1.0 dates to 2016-03-02, so the cadence is bursty rather than steady. There is an UPGRADE.markdown file at the top level of the repository, which is where breaking changes between major versions are documented. The README's own Laravel support list is effectively the upgrade guide's summary: moving from Laravel 11 to 12 requires at least v6.0.7, and moving to Laravel 13 requires v7.0.0. Budget for that coupling whenever you bump the framework. On licensing, the README carries a Packagist licence badge and a link to the package page, but the licence identifier is not stated in the README or the other repository files listed here. Check the composer.json licence field or the Packagist page before you ship, and route anything unusual to your own legal review rather than assuming a permissive default.
Editorial conclusion
Adopt lazychaser/laravel-nestedset for category trees, multi-depth menus and any hierarchy that is read far more often than it is restructured, and for Laravel versions the README maps to a release. Do not adopt it for comment threads, activity feeds or anything where users insert nodes continuously, because the nested set model renumbers on writes. Before committing, verify two things on a copy of the real data: run the consistency check described in the README's checking consistency section and confirm the tree reports as correct, and test the exact package version against your Laravel release, since the README ties each Laravel version to a minimum tag such as v6.0.7 for Laravel 12.
Frequently asked questions
Which Laravel versions does lazychaser/laravel-nestedset support?
The README maps each Laravel line to a minimum package version: Laravel 13 since v7.0.0, Laravel 12 since v6.0.7, Laravel 11.0 since v6.0.4, Laravel 10.0 since v6.0.2, Laravel 9.0 since v6.0.1 and Laravel 8.0 since v6.0.0, with older versions covered by v5, v4, v3 and v2.
How do I install lazychaser/laravel-nestedset?
It is installed through Composer as kalnoy/nestedset, which is the package name the README's Packagist badges point to. The README has an installation section for the remaining setup steps.
Does laravel-nestedset start a transaction when I move a node?
No. The README states that as of v4.2.0 a transaction is not automatically started, and that moving and inserting nodes includes several database queries, so it highly recommends wrapping those operations in transactions yourself.
How do I know a node actually moved after saving it in laravel-nestedset?
A successful save does not guarantee the node moved, because structural manipulations are deferred until save is called. The README says to call hasMoved after a successful save when your code depends on the new position.
Can I rebuild a whole tree at once with lazychaser/laravel-nestedset?
Yes. The README documents Category::rebuildTree($data, $delete), where nodes with an id are updated, nodes without one are created, a missing id throws ModelNotFoundException, and the second argument controls deletion of nodes absent from the data. Since 4.2.8 there is also rebuildSubtree for restricting the rebuild to a root's descendants.
Is lazychaser/laravel-nestedset a good fit for comment threads?
The README scopes nested sets to trees that are updated rarely and names multi-depth menus and shop categories as the intended applications, because updating requires renumbering. A structure with constant inserts is the case the model handles worst.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/lazychaser-laravel-nestedset)Star history chart for your README
[](https://hysenlabs.com/projects/lazychaser-laravel-nestedset)Was this useful?