PHPackages                             alex-kalanis/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. alex-kalanis/nested-tree

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alex-kalanis/nested-tree
========================

PHP Library to process nested tree structures

v2.3.0(4mo ago)01441MITPHPPHP &gt;=8.1.0CI passing

Since Apr 3Pushed 4mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (5)Versions (11)Used By (1)

Nested Tree
===========

[](#nested-tree)

[![Build Status](https://github.com/alex-kalanis/nested-tree/actions/workflows/code_checks.yml/badge.svg)](https://github.com/alex-kalanis/nested-tree/actions/workflows/code_checks.yml/badge.svg)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/077d410e1f8eb44f607726b2b55d91ac4e24fb3e476da7be80e5584406cbe94c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c65782d6b616c616e69732f6e65737465642d747265652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alex-kalanis/nested-tree/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/cf90ffe2739b0277e94932bc4edff1961e0cc6855aa2e102572e7538c3e60b50/68747470733a2f2f706f7365722e707567782e6f72672f616c65782d6b616c616e69732f6e65737465642d747265652f762f737461626c652e7376673f763d31)](https://packagist.org/packages/alex-kalanis/nested-tree)[![Minimum PHP Version](https://camo.githubusercontent.com/183804d09fec16ca7b6209b007250b7d8db1b915042feb093a9f20e6e1f25359/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e312d3838393242462e737667)](https://php.net/)[![Downloads](https://camo.githubusercontent.com/01526d2830515f50b0879aec74722cb958b096027695f7e9cadd9b6af479decf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c65782d6b616c616e69732f6e65737465642d747265652e7376673f7631)](https://packagist.org/packages/alex-kalanis/nested-tree)[![License](https://camo.githubusercontent.com/2a3393b62caa21a65dca0a88c63669802cc778f1aff75c31d2115780180ca07c/68747470733a2f2f706f7365722e707567782e6f72672f616c65782d6b616c616e69732f6e65737465642d747265652f6c6963656e73652e7376673f763d31)](https://packagist.org/packages/alex-kalanis/nested-tree)[![Code Coverage](https://camo.githubusercontent.com/f369308e6e7594fbdad292eccc15a972c7380b83b245d779541997ca15922048/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c65782d6b616c616e69732f6e65737465642d747265652f6261646765732f636f7665726167652e706e673f623d6d617374657226763d31)](https://scrutinizer-ci.com/g/alex-kalanis/nested-tree/?branch=master)

Library to work with Nested tree set. Rework of [Rundiz's library](https://github.com/Rundiz/nested-set).

About
-----

[](#about)

The PHP nested set model for create/read/update/delete the tree data structure (hierarchy).

It uses combination of nested and adjacency models. Also uses primary and foreign keys as simple integers, so you cannot use composed keys.

Requirements
------------

[](#requirements)

- PHP version 8.1 or higher

Basic usage
-----------

[](#basic-usage)

Basic usage is about to set `Support\TableSettings` with corresponding columns in your app and then pass it into the libraries. Next you need to extend `Support\Node`to describe your real table. For specific things like more columns you also need to set `Support\Options` class. The most code is then set via DI. You can see that in tests.

```
class MyNodes extends \kalanis\nested_tree\Support\Node
{
    public ?string $my_column = null;
}

class MyTable extends \kalanis\nested_tree\Support\TableSettings
{
    public string $tableName = 'my_menu';
}

$myNodes = new MyNodes();
$myTable = new MyTable();

// this is usually set via DI
$actions = new \kalanis\nested_tree\Actions(
    new \kalanis\nested_tree\NestedSet(
        new \kalanis\nested_tree\Sources\PDO\MySql(
            $yourPDOconnection,
            $myNodes,
            $myTable,
        ),
        $myNodes,
        $myTable,
    ),
);

// now work:

// repair the whole structure
$actions->fixStructure();

// move node in row
$actions->movePosition(25, 3);

// change parent node for the one chosen
$actions->changeParent(13, 7);
```

DB structure
------------

[](#db-structure)

This library need following columns or their equivalents on affected table:

- `id` - PK on table, cannot be zero, because that is the same as top/root node
- `parent_id` - FK to PK on the same table, can be null for top - depend on you DB schema, cannot be zero, because that is the same as top/root node
- `left` - left leaf of nested tree
- `right` - right leaf of nested tree
- `level` - how deep is it
- `position` - where it is against others in the level group

Each column can be set to different name by change in `TableSettings` class.

Running tests
-------------

[](#running-tests)

The `master` branch includes unit tests. If you just want to check that everything is working as expected, executing the unit tests is enough.

- `phpunit` - runs unit and functional tests

Caveats
-------

[](#caveats)

You must choose if you go with MariaDB or MySQL, because default implementation uses function [ANY\_VALUE()](https://jira.mariadb.org/browse/MDEV-10426) to go around the problem with non-standard `GROUP_BY` implementation. So you may either use MySQL 5.7+ or disable `ONLY_FULL_GROUP_BY` directive in MariaDB. Or write custom query source which itself will go around this particular problem.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance77

Regular maintenance activity

Popularity11

Limited adoption so far

Community9

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

Every ~31 days

Recently: every ~66 days

Total

10

Last Release

124d ago

Major Versions

v0.1.0 → v1.0.02025-04-03

v1.2.2 → v2.0.02025-12-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/499b0a541b787cdb31412f578c7b94c9790bcbee7de12c65b6101c6ce45ef6f0?d=identicon)[alex-kalanis](/maintainers/alex-kalanis)

---

Top Contributors

[![alex-kalanis](https://avatars.githubusercontent.com/u/59184183?v=4)](https://github.com/alex-kalanis "alex-kalanis (39 commits)")

---

Tags

phpnestedtreemenu

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[knplabs/knp-menu

An object oriented menu library

1.4k55.8M285](/packages/knplabs-knp-menu)[loophp/phptree

An implementation of tree data structure

981.8M2](/packages/loophp-phptree)[kartik-v/yii2-tree-manager

An enhanced tree management module with tree node selection and manipulation using nested sets.

156529.0k15](/packages/kartik-v-yii2-tree-manager)[efureev/laravel-trees

Multi-Tree structures for Laravel

14253.3k4](/packages/efureev-laravel-trees)[gilek/yii2-gtreetable

yii2-gtreetable is an extension of Yii 2 Framework, which is wrapper for bootstrap-gtreetable plug-in, on the other hand it provides functionality which allows to save the nodes states into database.

4111.8k](/packages/gilek-yii2-gtreetable)[asgardcms/menu-module

Menu module for AsgardCMS. Handles all the menus.

1030.4k2](/packages/asgardcms-menu-module)

PHPackages © 2026

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