PHPackages                             uccello/eloquent-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. uccello/eloquent-tree

ActiveLibrary

uccello/eloquent-tree
=====================

Eloquent Tree transforms a model into tree model for Laravel Eloquent ORM.

1.2.0(1y ago)141812MITPHP

Since Jan 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/uccellolabs/eloquent-tree)[ Packagist](https://packagist.org/packages/uccello/eloquent-tree)[ RSS](/packages/uccello-eloquent-tree/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (2)

eloquent-tree [![Latest Stable Version](https://camo.githubusercontent.com/5b86f22046d3a24d42ed10464e01fc25dfd83031a3713509f104185c4dd310d1/68747470733a2f2f706f7365722e707567782e6f72672f756363656c6c6f2f656c6f7175656e742d747265652f762f737461626c652e706e67)](https://packagist.org/packages/uccello/eloquent-tree) [![Total Downloads](https://camo.githubusercontent.com/0347210c27c3db2b50aaffc3b759ba11304b213675822dd65bda5b9c9d55192c/68747470733a2f2f706f7365722e707567782e6f72672f756363656c6c6f2f656c6f7175656e742d747265652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/uccello/eloquent-tree)
===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#eloquent-tree--)

Eloquent Tree transforms a model into tree model for Laravel Eloquent ORM.

This project is based on the original project made by [Adrian Skierniewski](https://github.com/AdrianSkierniewski/eloquent-tree). It was changed to be able to use this functionnality thanks to the `IsTree` trait, instead of extending the `Tree` model. It is useful if you want your model extends another class.

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Installation](#installation)
- [Migration](#migration)
- [Example usage](#example-usage)
- [Events](#events)
- [Support](#support)

\##Features

- Creating root, children and sibling nodes
- Getting children
- Getting descendants
- Getting ancestor
- Moving sub-tree
- Building tree on PHP side

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

[](#installation)

**For Laravel &gt;= 5.3**

Begin by installing this package through Composer. Edit your project's composer.json file to require uccello/eloquent-tree.

```
"require": {
    "uccello/eloquent-tree": "1.*"
},
"minimum-stability" : "stable"
```

Next, update Composer from the Terminal:

```
composer update

```

That's all now you can extend \\Gzero\\EloquentTree\\Model\\Tree in your project

Migration
---------

[](#migration)

Simply migration with all required columns that you could extend by adding new fields

```
Schema::create(
    'trees',
    function (Blueprint $table) {
        $table->increments('id');
        $table->string('path', 255)->nullable();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->integer('level')->default(0);
        $table->timestamps();
        $table->index(array('path', 'parent_id', 'level'));
        $table->foreign('parent_id')->references('id')->on('contents')->onDelete('CASCADE');
    }
);
```

Example usage
-------------

[](#example-usage)

- [Inserting and Updating new nodes](#inserting-and-updating-new-nodes)
- [Getting tree nodes](#getting-tree-nodes)
- [Finding Leaf nodes](#getting-leaf-nodes)
- [Map from array](#map-from-array)
- [Rendering tree](#rendering-tree)

### Inserting and updating new nodes

[](#inserting-and-updating-new-nodes)

```
$root = new Tree(); // New root
$root->setAsRoot();
$child = with(new Tree())->setChildOf($root); // New child
$sibling = new Tree();
$sibling->setSiblingOf($child); // New sibling
```

### Getting tree nodes

[](#getting-tree-nodes)

Leaf - returning root node

```
$leaf->findRoot();
```

Children - returning flat collection of children. You can use Eloquent query builder.

```
$collection = $root->children()->get();
$collection2 = $root->children()->where('url', '=', 'slug')->get();
```

Ancestors - returning flat collection of ancestors, first is root, last is current node. You can use Eloquent query builder. Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where

```
$collection = $node->findAncestors()->get();
$collection2 = $node->findAncestors()->where('url', '=', 'slug')->get();
```

Descendants - returning flat collection of descendants, first is current node, last is leafs. You can use Eloquent query builder. Of course there are no guarantees that the structure of the tree would be complete if you do the query with additional where

```
$collection = $node->findDescendants()->get();
$collection2 = $node->findDescendants()->where('url', '=', 'slug')->get();
```

Building tree structure on PHP side - if some nodes will be missing, these branches will not be built

```
$treeRoot = $root->buildTree($root->findDescendants()->get())
```

### Getting leaf nodes

[](#getting-leaf-nodes)

```
Tree::getLeaves();
```

### Map from array

[](#map-from-array)

Three new roots, first with descendants

```
 Tree::mapArray(
            array(
                array(
                    'children' => array(
                        array(
                            'children' => array(
                                array(
                                    'children' => array(
                                        array(
                                            'children' => array()
                                        ),
                                        array(
                                            'children' => array()
                                        )
                                    )
                                ),
                                array(
                                    'children' => array()
                                )
                            )
                        ),
                        array(
                            'children' => array()
                        )
                    )
                ),
                array(
                    'children' => array()
                ),
                array(
                    'children' => array()
                )
            )
 );
```

### Rendering tree

[](#rendering-tree)

You can render tree built by the function buildTree

```
 $html = $root->render(
        'ul',
        function ($node) {
            return '' . $node->title . '{sub-tree}';
        },
        TRUE
        );
 echo $html;
```

Events
------

[](#events)

All tree models have additional events:

- updatingParent
- updatedParent
- updatedDescendants

You can use them for example to update additional tables

Credits
-------

[](#credits)

- [Adrian Skierniewski](https://github.com/AdrianSkierniewski)
- [Uccello Labs](https://github.com/uccellolabs)
- [Jonathan SARDO](https://github.com/sardoj)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity55

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 ~651 days

Total

3

Last Release

624d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0dd3449bee600c805043068270d8c8d1fe7db9960f76e94b660d1192dfd3b3be?d=identicon)[sardoj](/maintainers/sardoj)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/uccello-eloquent-tree/health.svg)

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

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[namu/wirechat

A Laravel Livewire messaging app for teams with private chats and group conversations.

54324.5k](/packages/namu-wirechat)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)

PHPackages © 2026

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