PHPackages                             artisanweblab/nested-tree - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. artisanweblab/nested-tree

ActiveLibrary[Framework](/categories/framework)

artisanweblab/nested-tree
=========================

Nested Tree package for Laravel

v1.0.0(3y ago)037MITPHPPHP ^8.0

Since Jan 21Pushed 3y agoCompare

[ Source](https://github.com/ArtisanWebLab/nested-tree)[ Packagist](https://packagist.org/packages/artisanweblab/nested-tree)[ RSS](/packages/artisanweblab-nested-tree/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

Nested Tree for Laravel
-----------------------

[](#nested-tree-for-laravel)

The [nested set model](https://en.wikipedia.org/wiki/Nested_set_model) is an advanced technique for maintaining hierachies among models using `parent_id`, `nest_left`, `nest_right`, and `nest_depth` columns. To use a nested set model, apply the `ArtisanWebLab\NestedTree\Database\Traits\NestedTreeTrait` trait.

```
composer require artisanweblab/nested-tree
```

```
 $table->integer('parent_id')->nullable();
 $table->integer('nest_left')->nullable();
 $table->integer('nest_right')->nullable();
 $table->integer('nest_depth')->nullable();
```

```
use ArtisanWebLab\NestedTree\Database\Traits\NestedTreeTrait;

class Category extends Model
{
    use NestedTreeTrait;

    // You can change the column names used by declaring
    const PARENT_ID = 'my_parent_column';
    const NEST_LEFT = 'my_left_column';
    const NEST_RIGHT = 'my_right_column';
    const NEST_DEPTH = 'my_depth_column';
}
```

### Creating a root node

[](#creating-a-root-node)

By default, all nodes are created as roots:

```
$root = Category::create(['name' => 'Root category']);
```

Alternatively, you may find yourself in the need of converting an existing node into a root node:

```
$node->makeRoot();
```

You may also nullify it's `parent_id` column which works the same as `makeRoot'.

```
$node->parent_id = null;
$node->save();
```

### Inserting nodes

[](#inserting-nodes)

You can insert new nodes directly by the relation:

```
$child1 = $root->children()->create(['name' => 'Child 1']);
```

Or use the `makeChildOf` method for existing nodes:

```
$child2 = Category::create(['name' => 'Child 2']);
$child2->makeChildOf($root);
```

### Deleting nodes

[](#deleting-nodes)

When a node is deleted with the `delete` method, all descendants of the node will also be deleted. Note that the delete [model events](../database/model.md#model-events) will not be fired for the child models.

```
$child1->delete();
```

### Getting the nesting level of a node

[](#getting-the-nesting-level-of-a-node)

The `getLevel` method will return current nesting level, or depth, of a node.

```
// 0 when root
$node->getLevel()
```

### Moving nodes around

[](#moving-nodes-around)

There are several methods for moving nodes around:

- `moveLeft()`: Find the left sibling and move to the left of it.
- `moveRight()`: Find the right sibling and move to the right of it.
- `moveBefore($otherNode)`: Move to the node to the left of ...
- `moveAfter($otherNode)`: Move to the node to the right of ...
- `makeChildOf($otherNode)`: Make the node a child of ...
- `makeRoot()`: Make current node a root node.

---

Copyright (c) 2014-2020 Responsiv Pty Ltd, October CMS

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1212d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/31ccbd6b66f4496f505930e8154d0858a3a63304907315ff409d6fcef678222d?d=identicon)[ArtisanWebLab](/maintainers/ArtisanWebLab)

---

Top Contributors

[![OleksandrWebLab](https://avatars.githubusercontent.com/u/20097205?v=4)](https://github.com/OleksandrWebLab "OleksandrWebLab (3 commits)")

---

Tags

laravelmodelsnestedtree

### Embed Badge

![Health badge](/badges/artisanweblab-nested-tree/health.svg)

```
[![Health](https://phpackages.com/badges/artisanweblab-nested-tree/health.svg)](https://phpackages.com/packages/artisanweblab-nested-tree)
```

###  Alternatives

[codewithdennis/larament

Larament is a time-saving starter kit to quickly launch Laravel 13.x projects. It includes FilamentPHP 5.x pre-installed and configured, along with additional tools and features to streamline your development workflow.

3691.5k](/packages/codewithdennis-larament)[ecotone/laravel

Laravel integration for Ecotone

21307.6k3](/packages/ecotone-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
