Build a product category tree for an online shop where customers can browse top-level and subcategory pages.
Create a multi-level navigation menu stored in a database that can be fetched with a single query.
Save the result of a drag-and-drop tree editor on the front end into the database in one call using rebuildTree.
Add breadcrumb navigation by retrieving all ancestors of the current page with one method call.
Requires running a migration to add nested set columns and choosing the correct package version for your Laravel version (4 through 8).
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.
← lazychaser on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.