explaingit

lazychaser/laravel-nestedset

3,796PHPAudience · developerComplexity · 3/5Setup · moderate

TLDR

A Laravel package that stores parent-child data like category trees and nav menus in a database using the Nested Set Model, enabling fast single-query reads of entire branches.

Mindmap

mindmap
  root((NestedSet))
    What it does
      Store tree data
      Fast hierarchy reads
      Single query access
    Tech stack
      PHP Laravel
      MySQL database
      Composer install
    Use cases
      Category trees
      Nav menus
      Breadcrumbs
    Features
      Move nodes
      Get ancestors
      Rebuild tree
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Build a product category tree for an online shop where customers can browse top-level and subcategory pages.

USE CASE 2

Create a multi-level navigation menu stored in a database that can be fetched with a single query.

USE CASE 3

Save the result of a drag-and-drop tree editor on the front end into the database in one call using rebuildTree.

USE CASE 4

Add breadcrumb navigation by retrieving all ancestors of the current page with one method call.

Tech stack

PHPLaravelMySQLComposer

Getting it running

Difficulty · moderate Time to first run · 30min

Requires running a migration to add nested set columns and choosing the correct package version for your Laravel version (4 through 8).

In plain English

This is a PHP package for the Laravel web framework that helps store tree-shaped data in a regular database table. A tree in software terms is any data that has a parent-child hierarchy: categories with subcategories, a folder structure, a comment thread with replies, or a navigation menu with nested items. The approach it uses is called the Nested Set Model. A relational database (the kind that most web applications use) stores data in flat rows and columns, which is not naturally suited to hierarchies. A simple approach is to give each row a parent_id column pointing to its parent row, but that gets slow when you need to ask questions like "give me all descendants of this node" because the database has to run many queries to walk down the tree. The Nested Set Model works differently: it assigns two numbers to each node based on a traversal of the tree, and those numbers let the database answer hierarchy questions in a single fast query. The tradeoff is that inserting or moving nodes requires updating many rows, so it works best when the tree changes infrequently and reads are frequent. The package adds tree capabilities directly onto Laravel database models. Once installed, your model can call methods to insert a node as a child of another node, move it before or after a sibling, retrieve all its ancestors (useful for breadcrumb navigation), retrieve all its descendants, or rebuild the entire tree from an array of data. It also includes tools to check whether the stored numbers are consistent and to fix them if they get out of sync. Common uses described in the README include multi-level navigation menus and product category trees for online shops. The package supports building a full tree from an array in one call, which is useful for saving the results of a drag-and-drop tree editor on the front end. Installation uses Composer, the standard PHP package manager. The package is compatible with Laravel versions 4 through 8, with different version numbers of the package covering different Laravel generations.

Copy-paste prompts

Prompt 1
Using laravel-nestedset, help me create a Category Eloquent model with the NodeTrait, write the migration that adds the nested set columns, and insert a sample tree of three levels.
Prompt 2
Show me how to retrieve all descendants of a given Category node with laravel-nestedset and render them as a nested HTML unordered list in a Blade template.
Prompt 3
I want to move a category to become a child of a different parent node using laravel-nestedset. What method do I call, what does the code look like, and does it update all the nested set numbers automatically?
Prompt 4
How do I use laravel-nestedset's rebuildTree method to save a full category tree that came from a front-end drag-and-drop editor as a nested JSON array?
Prompt 5
After importing data manually into my Laravel app, my nested set numbers are out of sync. How do I use the package's built-in checker and fixer to detect and repair the inconsistency?
Open on GitHub → Explain another repo

← lazychaser on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.