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

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

hsharghi/nestablecollection
===========================

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

1.1.20(5y ago)0603MITPHP

Since Feb 18Pushed 1y agoCompare

[ Source](https://github.com/hsharghi/NestableCollection)[ Packagist](https://packagist.org/packages/hsharghi/nestablecollection)[ GitHub Sponsors](https://github.com/typicms)[ RSS](/packages/hsharghi-nestablecollection/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (25)Used By (0)

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

[](#nestablecollection)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

A Laravel Package that extends collections to handle 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 = [
    '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 :

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

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.

Change the name of subcollections
---------------------------------

[](#change-the-name-of-subcollections)

By default, the name of the subcollections is **items**, but you can change it by calling the `childrenName($name)` method : For example if you want your subcollections being named **children**:

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

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()->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()->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()->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();
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 81.4% 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 ~88 days

Recently: every ~61 days

Total

24

Last Release

2057d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2873823?v=4)[Hadi Sharghi](/maintainers/hsharghi)[@hsharghi](https://github.com/hsharghi)

---

Top Contributors

[![sdebacker](https://avatars.githubusercontent.com/u/134503?v=4)](https://github.com/sdebacker "sdebacker (79 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)")[![aimalamiri](https://avatars.githubusercontent.com/u/34779928?v=4)](https://github.com/aimalamiri "aimalamiri (1 commits)")[![izshreyansh](https://avatars.githubusercontent.com/u/13690290?v=4)](https://github.com/izshreyansh "izshreyansh (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)")

---

Tags

laraveleloquentcollectiontreeadjacency listnested-setTypiCMS

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/hsharghi-nestablecollection/health.svg)](https://phpackages.com/packages/hsharghi-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)[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)
