PHPackages                             gregoryduckworth/nestablecollection - 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. gregoryduckworth/nestablecollection

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

gregoryduckworth/nestablecollection
===================================

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

v1.1.9(8y ago)013MITPHP

Since Feb 18Pushed 7y agoCompare

[ Source](https://github.com/gregoryduckworth/NestableCollection)[ Packagist](https://packagist.org/packages/gregoryduckworth/nestablecollection)[ RSS](/packages/gregoryduckworth-nestablecollection/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)DependenciesVersions (14)Used By (0)

NestableCollection
==================

[](#nestablecollection)

[![SensioLabsInsight](https://camo.githubusercontent.com/b5778b3fe39d52eebd3149066c11b3f438930a283076ced08ea978decf579f74/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38646333343962342d393531642d343039382d616633612d6332393131393337613930312f6d696e692e706e67)](https://insight.sensiolabs.com/projects/8dc349b4-951d-4098-af3a-c2911937a901)[![StyleCI](https://camo.githubusercontent.com/814f74ba24f3dfc6a50e998d7d2e4c75127af31a89dc42cb1cf171ca8e4a9a08/68747470733a2f2f7374796c6563692e696f2f7265706f732f33303937313831322f736869656c64)](https://styleci.io/repos/30971812)

A Laravel/Lumen Package that extends Collection to handle unlimited nested items following adjacency list model.

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

[](#installation)

Run `composer require typicms/nestablecollection`

Usage
-----

[](#usage)

The model must have a **parent\_id** attributes :

```
protected $fillable = array(
    'parent_id',
    // …
}
```

and must use the following trait:

```
use TypiCMS\NestableTrait;
```

Now each time you get a collection of that model, it will be an instance of **TypiCMS\\NestableCollection** in place of **Illuminate\\Database\\Eloquent\\Collection**.

If you want a tree of models, simply call the nest method on a collection ordered by parent\_id asc and pass the column on which you want the nesting to occurr :

```
Model::orderBy('parent_id')->get()->nest('parent_id');
```

Of course you will probably want a position column as well. So you will have to order first by parent\_id asc and then by position asc.

Indented and flattened list
---------------------------

[](#indented-and-flattened-list)

`listsFlattened()` method generate the tree as a flattened list with id as keys and title as values, perfect for select/option, for example :

```
[
    '22' => 'Item 1 Title',
    '10' => '    Child 1 Title',
    '17' => '    Child 2 Title',
    '14' => 'Item 2 Title',
]
```

To use it, first call the `nest()` method, followed by the `listsFlattened()` method:

```
Model::orderBy('parent_id')->get()->nest('parent_id')->listsFlattened();
```

By default it will look for a `title` column. You can send a custom column name as first parameter:

```
Model::orderBy('parent_id')->get()->nest('parent_id')->listsFlattened('name');
```

Four spaces are used to indent by default, to use your own use the `setIndent()` method, followed by the `listsFlattened()` method:

```
Model::orderBy('parent_id')->get()->nest('parent_id')->setIndent('> ')->listsFlattened();
```

Results:

```
[
    '22' => 'Item 1 Title',
    '10' => '> Child 1 Title',
    '17' => '> Child 2 Title',
    '14' => 'Item 2 Title',
]
```

Nesting a subtree
-----------------

[](#nesting-a-subtree)

This package remove items that have missing ancestor, this doesn’t allow you to nest a branch of a tree. To avoid this, you can use the `noCleaning()` method:

```
Model::orderBy('parent_id')->get()->noCleaning()->nest('parent_id');
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 83.1% 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 ~94 days

Recently: every ~88 days

Total

13

Last Release

2969d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3cab14bc2b88feb8bd98f56637a91c50e2ba47a81489703f457a8bcb4729b743?d=identicon)[gregoryduckworth](/maintainers/gregoryduckworth)

---

Top Contributors

[![sdebacker](https://avatars.githubusercontent.com/u/134503?v=4)](https://github.com/sdebacker "sdebacker (49 commits)")[![TonyRouse](https://avatars.githubusercontent.com/u/1280950?v=4)](https://github.com/TonyRouse "TonyRouse (5 commits)")[![nWidart](https://avatars.githubusercontent.com/u/882397?v=4)](https://github.com/nWidart "nWidart (4 commits)")[![gregoryduckworth](https://avatars.githubusercontent.com/u/2647926?v=4)](https://github.com/gregoryduckworth "gregoryduckworth (1 commits)")

---

Tags

laraveleloquentcollectiontreeadjacency listnested-setTypiCMS

### Embed Badge

![Health badge](/badges/gregoryduckworth-nestablecollection/health.svg)

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

###  Alternatives

[typicms/nestablecollection

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

88327.2k19](/packages/typicms-nestablecollection)[baril/bonsai

An implementation of the Closure Tables pattern for Eloquent.

3593.5k](/packages/baril-bonsai)[jiaxincui/closure-table

A closure table tree package for laravel eloquent

1138.4k](/packages/jiaxincui-closure-table)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)

PHPackages © 2026

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