PHPackages                             codefocus/nestedset - 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. [Database &amp; ORM](/categories/database)
4. /
5. codefocus/nestedset

ActiveLibrary[Database &amp; ORM](/categories/database)

codefocus/nestedset
===================

Nested set implementation for Eloquent models in Laravel.

216263PHP

Since May 11Pushed 8y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

NestedSet
=========

[](#nestedset)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ca946e4cdc53b23cfcaf2cc3111ae9ad24561bf3f28f7d664232695f11bdade2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6465666f6375732f6e65737465647365742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codefocus/nestedset)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/81976b186a3e0af5ccfc4d496076232bedad27845051c8779384bee0a5033358/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6465666f6375732f6e65737465647365742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/codefocus/nestedset)[![Total Downloads](https://camo.githubusercontent.com/2e66fac294ef95eaf762a39928a9d0a852f4ee3d687aa2fc64b71c282241b064/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6465666f6375732f6e65737465647365742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codefocus/nestedset)

Simple to use implementation of the nested set structure, for Eloquent models in Laravel.

CAUTION
-------

[](#caution)

**This package is currently INCOMPLETE and deployment in a production environment is NOT RECOMMENDED**

To learn more about the nested set structure, please refer to "[Efficient tree retrieval in Laravel using the nested set structure](http://www.codefocus.ca/blog/efficient-tree-retrieval-in-laravel-using-the-nested-set)" on [codefocus.ca](http://www.codefocus.ca/).

Table of contents
-----------------

[](#table-of-contents)

- [Install](#install)
- [Configuration](#configuration)
    - [Enabling the nested set functionality in your model](#enabling-the-nested-set-functionality-in-your-model)
    - [Database columns](#database-columns)
    - [Database indexes](#database-indexes)
- [Usage](#usage)
    - [Building a tree](#building-a-tree)
        - [Building a new tree from an existing parent-child based data structure](#building-a-new-tree-from-an-existing-parent-child-based-data-structure)
        - [Rebuilding the tree under an existing node](#rebuilding-the-tree-under-an-existing-node)
        - [Adding a node](#adding-a-node)
        - [Moving a node](#moving-a-node)
        - [Removing a node](#removing-a-node)
- [Testing](#testing)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)

Install
-------

[](#install)

Via Composer

```
$ composer require codefocus/nestedset
```

Configuration
-------------

[](#configuration)

### Enabling the nested set functionality in your model

[](#enabling-the-nested-set-functionality-in-your-model)

To implement the NestedSetTrait, simply `use` it in your model:

```
class YourModel extends \Illuminate\Database\Eloquent\Model {

    use \Codefocus\NestedSet\NestedSetTrait;
    ...

}
```

### Database columns

[](#database-columns)

The Trait expects database columns to be present for (at least) your Model's `left`, `right` and `parent_id` fields. The names of these fields can be configured per Model, by setting the following protected variables in the Model that uses it:

```
    protected $nestedSetColumns = [
    //  Which column to use for the "left" value.
    //	Default: left
        'left' => 'left',

    //  Which column to use for the "right" value.
    //	Default: right
        'right' => 'right',

    //  Which column to point to the parent's PK.
    //  Null is allowed. This will remove the ability to rebuild the tree.
    //	Default: parent_id
        'parent' => 'parent_id',

    //  Which column to use for the node's "depth", or level in the tree.
    //  Null is allowed.
    //    ! When restricting the tree by depth, each node's depth will be
    //      calculated automatically. This is not recommended for large trees.
    //	Default: null
        'depth' => null,

    //  When a table can hold multiple trees, we need to specify which field
    //  uniquely identifies which tree we are operating on.
    //  E.g. in the case of comments, that could be "thread_id" or "post_id".
    //  Null is allowed. NestedSetTrait will assume there is only one tree.
    //	Default: null
        'group' => null,
    ];
```

### Database indexes

[](#database-indexes)

Indexes are highly recommended on these fields (or the ones configured in `$nestedSetColumns`):

- `left`, `right`, `group`, `depth`
- `left`, `group`, `depth`
- `parent_id`

If you are not using `depth` and `group`, these indexes will suffice:

- `left`, `right`
- `parent_id`

Usage
-----

[](#usage)

### Building a tree

[](#building-a-tree)

#### Building a new tree from an existing parent-child based data structure

[](#building-a-new-tree-from-an-existing-parent-child-based-data-structure)

**@TODO: incomplete**

Use your data's existing parent → child hierarchy to construct a new tree (or multiple trees, if you have configured the `$nestedSetColumns['group']` column in your model).

This may take a while, depending on the size of your data set!

```
YourModel::buildNewTree();
```

#### Rebuilding the tree under an existing node

[](#rebuilding-the-tree-under-an-existing-node)

**@TODO: incomplete**

Use your data's existing parent → child hierarchy to (re)construct (part of the) tree, from the current node downward.

```
$yourModelInstance->buildTree();
```

#### Adding a node

[](#adding-a-node)

Adding a node to the tree requires literally no work. Just save a model instance as usual, and the Trait will automagically adjust the tree structure.

```
$yourModelInstance->save();
```

#### Moving a node

[](#moving-a-node)

**@TODO: incomplete**

Moving a node from one parent to another (or no parent) is handled in the same way. When the Trait sees that a model instance's `parent_id` (or the column name configured in `$nestedSetColumns['parent']`) value has changed, the tree structure is adjusted accordingly.

```
$yourModelInstance->parent_id = $newParent->id;
$yourModelInstance->save();
```

#### Removing a node

[](#removing-a-node)

**@TODO: incomplete**

Deleting a node from the tree is also automated by the Trait. When you delete a model instance as usual, the Trait will adjust the tree structure.

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

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Menno van Ens](https://github.com/codefocus)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1cd3f45eae9b147c79013baf9779842a8256bd56cdd5e1408b1f9aac9ff11aa0?d=identicon)[codefocus](/maintainers/codefocus)

### Embed Badge

![Health badge](/badges/codefocus-nestedset/health.svg)

```
[![Health](https://phpackages.com/badges/codefocus-nestedset/health.svg)](https://phpackages.com/packages/codefocus-nestedset)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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