atayahmet/laravel-nestable
Laravel Nestable provides a simple trait for managing hierarchical Eloquent models. Build nested menus, categories, and trees with easy reordering, moving nodes, and querying parents, children, and descendants.
Install via Composer (composer require atayahmet/laravel-nestable) and register the service provider if you are on Laravel 5.x (package predates auto-discovery). Add the migration columns the package expects — this is a Nested Set implementation, so your table needs _lft, _rgt, parent_id, and (typically) depth/level. The readme shows the exact helper; if you are hand-writing the migration, mirror those column names or the trait's queries will silently misbehave.
Add the provided trait (e.g. NestableTrait) to your Category model. That is the whole integration surface — the trait exposes scopes and instance methods for tree queries and node moves.
First use case that pays for itself immediately: a category/menu table where the read is a full tree or a subtree, rendered recursively. Seed a handful of rows by using the package's "append child" helpers rather than inserting raw rows, since _lft/_rgt values must be maintained as a contiguous sequence. Then call the tree/subtree scope and pass the collection into a recursive Blade partial. That covers navigation menus, breadcrumbs, and category browse pages — the three places developers reach for a nested set model.
where parent_id = ? in a loop. Fetch the whole subtree (or full tree) in one query via the nested-set scope and shape it in PHP. This is the entire value proposition — one indexed query (_llft between ? and ?) replaces N recursive queries._lft/_rgt. Wrap the move and adjacent logging in a DB transaction; nested set updates touch many rows.[$parentId => [$children]]) to a recursive Blade partial. Avoid lazy-loading children relations in views — you will recreate the N+1 the package exists to avoid.scope_id (or similar) column and always constrain the tree queries by it. Note the package's default scopes may not know about your custom column — verify before relying on it.parent_id and make the client reconstruct it.getKeyName, mass assignment, withoutGlobalScopes) early. If it breaks, a fork is usually cheaper than migrating to kalnoy/nestedset, which is the actively maintained successor and worth considering up front._lft/_rgt must be a contiguous, non-overlapping sequence. A crash mid-move, a manual SQL edit, or a bulk insert bypassing the trait leaves an inconsistent tree that mostly works. Always run the package's rebuild/repair routine after data migrations, and add a sanity check (max _rgt vs row count, no overlaps) to your test suite.parent_id and now-invalid lft/rgt ranges are a common post-mortem._lft, _rgt, depth are conventional but check your other packages (e.g. auditing, tagging) before naming.id, parent_id, _lft, _rgt, depth sorted by _lft. Gaps, overlaps, or _rgt <= _lft immediately point to the broken node — don't debug through the Blade layer.How can I help you explore Laravel packages today?