PHPackages                             ibelousov/advanced-nested-set - 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. ibelousov/advanced-nested-set

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

ibelousov/advanced-nested-set
=============================

1.0.0(3y ago)0184MITPHPPHP ^8.0

Since Jan 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ibelousov/advanced-nested-set)[ Packagist](https://packagist.org/packages/ibelousov/advanced-nested-set)[ RSS](/packages/ibelousov-advanced-nested-set/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (0)

Advanced nested set
===================

[](#advanced-nested-set)

Attention
---------

[](#attention)

**FOR NOW POSTGRESQL, MYSQL AND SQLITE IS ONLY SUPPORTED DATABASES**

Overview
--------

[](#overview)

Advanced nested set is laravel package, that can be used for handling nested tree sets like this:

```
Category::first()->descendants; // print all nested categories of category
```

or like this

```
Category::whereHas('parents', function($query) {
    $query->where('name', 'vegetables');
});
```

Installation
------------

[](#installation)

Use the package manager composer to install it

```
composer require ibelousov/advanced-nested-set
```

Usage
-----

[](#usage)

### Setup

[](#setup)

So, you have Category model and want to add a nested set to it. For that purpose you can:

#### 1) Create and execute migration

[](#1-create-and-execute-migration)

```
    public function up()
    {
        Schema::table('categories', function (Blueprint $table) {
             $table->advancedNestedSet(); // create columns lft,rgt,depth,parent_id,deleted_at
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('categories', function (Blueprint $table) {
             $table->dropAdvancedNestedSet(); // delete columns lft,rgt,depth,parent_id,deleted_at
        });
    }
```

#### 2) add AdvancedNestedSet to model:

[](#2-add-advancednestedset-to-model)

```
use \Ibelousov\AdvancedNestedSet\AdvancedNestedSet;

class Category extends Model
{
    use AdvancedNestedSet;

    protected $fillable = [/*...Your columns..*/ 'lft','rgt','parent_id','depth','distance'];
}
```

#### 3) fix nested set(if there are records in Category)

[](#3-fix-nested-setif-there-are-records-in-category)

```
    php artisan advanced-nested-set:fix categories
```

#### 4) if already used "ADVANCED\_NESTED\_SET\_LOCK\_" prefix for blocking on CRUD doesn't fit your needs:

[](#4-if-already-used-advanced_nested_set_lock_-prefix-for-blocking-on-crud-doesnt-fit-your-needs)

add to .env

```
ADVANCED_NESTED_LOCK_NAME="NEW_LOCK_NAME"
```

#### 5) Fine tune time for blocking

[](#5-fine-tune-time-for-blocking)

add to .env

```
ADVANCED_NESTED_LOCK_WAIT=100 # Seconds waiting for atomic blocking(DEFAULT: 30)
ADVANCED_NESTED_LOCK_DELAY=9999999 # Microseconds waiting after blocking(DEFAULT: 10000)
```

### Create/Update/Delete nodes

[](#createupdatedelete-nodes)

- To create node:

```
$node = Category::create([...]); // Root node
```

- To create node inside parent::

```
Category::create(['parent_id' => $node->id]); // Child of Root node
```

- To move node from parent to root

```
$category->update(['parent_id' => null]);
```

- To move node to another parent

```
$category->update(['parent_id' => $parentId]);
```

- To move node **within** one parent

```
$category->moveAfter($category2);
```

### Relations

[](#relations)

```
// descendants of category
Category::first()->descendants;
```

```
// descendants of category and category itself
Category::first()->descendants_and_self;
```

```
// parents of category
Category::first()->parents;
```

```
// parents and self category of category
Category::first()->parents_and_self;
```

```
// parent of category
Category::first()->parent;
```

```
// children of category
Category::first()->children;
```

### Methods

[](#methods)

```
// Is nested set correct
Category::isCorrect();
```

```
// Get errors
Category::errors();
```

```
// Convert to treeStructure
Category::get()->toTree();

// Convert to tree and than to array
Category::get()->toTree()->toArray();
```

### Console commands

[](#console-commands)

#### Fix table

[](#fix-table)

To fix nested set in table you can use:

php artisan advanced-nested-set:fix tablename

You can add this command to schedule to hold nested set in correct state

#### Check table

[](#check-table)

To check nested set in table you can use:

php artisan advanced-nested-set:check tablename --verbose

### Important

[](#important)

As this package uses Cache, for correct handling between atomic lock processes it's suggested to use redis/memcached or other inmemory databases for speed and accuracy

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.9% 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

Every ~104 days

Total

5

Last Release

1151d ago

Major Versions

0.0.3 → 1.0.02023-03-15

### Community

Maintainers

![](https://www.gravatar.com/avatar/44dd3a2ed3993fbdc2d3ba24f6c94847dcee0d3dded005574363bc08452c0b75?d=identicon)[ibelousov](/maintainers/ibelousov)

---

Top Contributors

[![ibelousov](https://avatars.githubusercontent.com/u/60745119?v=4)](https://github.com/ibelousov "ibelousov (46 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

### Embed Badge

![Health badge](/badges/ibelousov-advanced-nested-set/health.svg)

```
[![Health](https://phpackages.com/badges/ibelousov-advanced-nested-set/health.svg)](https://phpackages.com/packages/ibelousov-advanced-nested-set)
```

###  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.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

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

Use ramsey/uuid as a Doctrine field type.

90340.3M208](/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)
