Laravel Nestedset
Nested set trees for Laravel Rust models: a subtree or every ancestor in one indexed query, moves with a fixed number of updates, and writes that lock their tree.
Product categories, site menus, comment threads, org charts. A whole subtree or every ancestor of
a node is one indexed query, a subtree moves with a fixed number of UPDATEs, and every write
locks its tree, so concurrent writers can't corrupt it.
Features
- Fast reads: descendants, ancestors, children, siblings and leaves, each one indexed query
- Plain builders: every tree query is a Laravel Rust model
Builder, so filter, paginate or count it further - Safe writes: create, move, copy and delete nodes; each write locks its tree
- No N+1: relatives of a whole list of nodes in one query
- Ready for the UI: a nested tree for JSON and templates, a parent select for admin forms, saving a drag-and-drop tree editor
- Several trees in one table, such as site menus, and an optional stored depth
- Check and fix a tree that was edited by hand
- Tested on SQLite, PostgreSQL and MySQL
Installation
[dependencies]
laravel-nestedset = "0.3"
Quick start
Add the columns in a migration:
use laravel_nestedset::NestedSetBlueprint;
tx.schema()
.create("categories", |table| {
table.id();
table.string("name");
table.nested_set();
table.timestamps();
})
.await?;
Implement NestedSet for the model, then query the tree:
shop.descendants().get().await?; // Phones, Android, iPhone, Laptops, Tablets
android.ancestors().get().await?; // Shop, Phones: breadcrumbs
shop.children().get().await?; // Phones, Laptops, Tablets
Category::leaves().get().await?; // Android, iPhone, Laptops, Tablets, Blog
The full guide, from moving and copying subtrees to concurrency and performance, is in the README on GitHub.