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

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

typicms/nestablecollection
==========================

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

6.0.1(2mo ago)88327.2k↓21.4%29[2 issues](https://github.com/typicms/nestablecollection/issues)19MITPHPPHP ^8.3

Since Feb 18Pushed 1mo ago6 watchersCompare

[ Source](https://github.com/typicms/nestablecollection)[ Packagist](https://packagist.org/packages/typicms/nestablecollection)[ GitHub Sponsors](https://github.com/typicms)[ RSS](/packages/typicms-nestablecollection/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (16)Versions (38)Used By (19)

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

[](#nestablecollection)

[![Software License](https://camo.githubusercontent.com/e3a212c4f32575f60745eb3ee25ae1ac6f070df3d1daf014673b4b5bbff7ad78/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f54797069434d532f4e65737461626c65436f6c6c656374696f6e)](https://github.com/TypiCMS/NestableCollection/blob/master/LICENCE)[![Tests](https://github.com/TypiCMS/NestableCollection/actions/workflows/tests.yml/badge.svg)](https://github.com/TypiCMS/NestableCollection/actions/workflows/tests.yml)[![PHPStan](https://github.com/TypiCMS/NestableCollection/actions/workflows/phpstan.yml/badge.svg)](https://github.com/TypiCMS/NestableCollection/actions/workflows/phpstan.yml)[![Pint](https://github.com/TypiCMS/NestableCollection/actions/workflows/pint.yml/badge.svg)](https://github.com/TypiCMS/NestableCollection/actions/workflows/pint.yml)[![Rector](https://github.com/TypiCMS/NestableCollection/actions/workflows/rector.yml/badge.svg)](https://github.com/TypiCMS/NestableCollection/actions/workflows/rector.yml)

A Laravel package that extends Eloquent collections to handle nested items following the adjacency list model.

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

[](#requirements)

- PHP ^8.3
- Laravel 12 or 13

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

[](#installation)

```
composer require typicms/nestablecollection
```

Usage
-----

[](#usage)

The model must have a `parent_id` attribute:

```
protected $fillable = [
    'parent_id',
    // …
];
```

and must use the `NestableTrait`:

```
use TypiCMS\NestableTrait;

class Category extends Model
{
    use NestableTrait;
}
```

Now each time you retrieve a collection of that model, it will be an instance of `TypiCMS\NestableCollection` instead of `Illuminate\Database\Eloquent\Collection`.

To get a tree of models, call the `nest()` method on a collection ordered by `parent_id`:

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

You will probably want a `position` column as well, so order first by `parent_id` then by `position`:

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

Custom parent column
--------------------

[](#custom-parent-column)

By default, the parent column is `parent_id`. You can change it with the `parentColumn()` method:

```
$collection->parentColumn('category_id')->nest();
```

Custom children name
--------------------

[](#custom-children-name)

By default, subcollections are named `items`. You can change it with the `childrenName()` method:

```
$collection->childrenName('children')->nest();
```

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

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

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

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

First call `nest()`, then `listsFlattened()`:

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

By default it looks for a `title` column. Pass a custom column name as the first parameter:

```
$collection->nest()->listsFlattened('name');
```

Four spaces are used to indent by default. Use `setIndent()` to customize:

```
$collection->nest()->setIndent('> ')->listsFlattened();
```

Result:

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

Fully qualified flattened list
------------------------------

[](#fully-qualified-flattened-list)

The `listsFlattenedQualified()` method builds full paths instead of indentation:

```
$collection->nest()->setIndent(' / ')->listsFlattenedQualified();
```

Result:

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

Setting parent relations
------------------------

[](#setting-parent-relations)

The `setParents()` method sets the `parent` relation on each nested item, so you can traverse up the tree without querying the database:

```
$nested = Category::orderBy('parent_id')->get()->nest()->setParents();

$nested[0]->items[0]->parent; // Returns the parent model
```

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

[](#nesting-a-subtree)

By default, items with a missing ancestor are removed. To nest a branch of a tree, use the `noCleaning()` method:

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

Testing
-------

[](#testing)

```
composer require pestphp/pest --dev
vendor/bin/pest
```

License
-------

[](#license)

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

###  Health Score

69

—

FairBetter than 100% of packages

Maintenance87

Actively maintained with recent releases

Popularity50

Moderate usage in the ecosystem

Community34

Small or concentrated contributor base

Maturity90

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 82.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 ~115 days

Recently: every ~191 days

Total

36

Last Release

67d ago

Major Versions

1.2.0 → 2.0.02022-04-26

2.0.1 → 3.0.02023-03-20

3.0.0 → 4.0.02024-02-05

4.0.1 → 5.0.02025-03-03

5.0.0 → 6.0.02026-03-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/b93740e4c6b40a6219441696db5db9c4b635dad91339c1893dfe0813434e8d94?d=identicon)[Samuel De Backer](/maintainers/Samuel%20De%20Backer)

---

Top Contributors

[![sdebacker](https://avatars.githubusercontent.com/u/134503?v=4)](https://github.com/sdebacker "sdebacker (96 commits)")[![TonyRouse](https://avatars.githubusercontent.com/u/1280950?v=4)](https://github.com/TonyRouse "TonyRouse (5 commits)")[![shankhadevpadam](https://avatars.githubusercontent.com/u/3838722?v=4)](https://github.com/shankhadevpadam "shankhadevpadam (5 commits)")[![nWidart](https://avatars.githubusercontent.com/u/882397?v=4)](https://github.com/nWidart "nWidart (4 commits)")[![Radovenchyk](https://avatars.githubusercontent.com/u/107338472?v=4)](https://github.com/Radovenchyk "Radovenchyk (2 commits)")[![izshreyansh](https://avatars.githubusercontent.com/u/13690290?v=4)](https://github.com/izshreyansh "izshreyansh (1 commits)")[![aimalamiri](https://avatars.githubusercontent.com/u/34779928?v=4)](https://github.com/aimalamiri "aimalamiri (1 commits)")[![hsharghi](https://avatars.githubusercontent.com/u/2873823?v=4)](https://github.com/hsharghi "hsharghi (1 commits)")[![frezno](https://avatars.githubusercontent.com/u/842148?v=4)](https://github.com/frezno "frezno (1 commits)")[![enlightened88](https://avatars.githubusercontent.com/u/198119344?v=4)](https://github.com/enlightened88 "enlightened88 (1 commits)")

---

Tags

adjacency-listlaravelnested-settree-structuretypicmslaraveleloquentcollectiontreeadjacency listnested-setTypiCMS

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[baril/bonsai

An implementation of the Closure Tables pattern for Eloquent.

3593.5k](/packages/baril-bonsai)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[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)
