PHPackages                             robertmarney/lara-hierarchial-collections - 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. robertmarney/lara-hierarchial-collections

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

robertmarney/lara-hierarchial-collections
=========================================

Transforms flat collections to a nested hierarchy

1.2.0(1y ago)342.2k1[1 PRs](https://github.com/robertmarney/lara-hierarchial-collections/pulls)MITPHPPHP ^8.0|^8.1|^8.2|^8.3|^8.4CI passing

Since Sep 10Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/robertmarney/lara-hierarchial-collections)[ Packagist](https://packagist.org/packages/robertmarney/lara-hierarchial-collections)[ Docs](https://github.com/robertmarney/lara-hierarchial-collections)[ RSS](/packages/robertmarney-lara-hierarchial-collections/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (14)Used By (0)

Transforms flat collections to a nested hierarchy
=================================================

[](#transforms-flat-collections-to-a-nested-hierarchy)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b91720a8bda4cf05c9976c1d0085289ef61dd824cd2c0aa1b7520f5e798afb98/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f626572746d61726e65792f6c6172612d686965726172636869616c2d636f6c6c656374696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robertmarney/lara-hierarchial-collections)[![GitHub Tests Action Status](https://camo.githubusercontent.com/26e6c66461dfc448d3fec7ed937519d6089c66adc6d0cf74ab9dba30eab95aa6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f726f626572746d61726e65792f6c6172612d686965726172636869616c2d636f6c6c656374696f6e732f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/robertmarney/lara-hierarchial-collections/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b97ee15fb507f2eacbf39e8362dcc056d2ffaca2e9e4238cf946bb91edefdbfb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f726f626572746d61726e65792f6c6172612d686965726172636869616c2d636f6c6c656374696f6e732f466978253230504850253230636f64652532307374796c652532306973737565733f6c6162656c3d636f64652532307374796c65)](https://github.com/robertmarney/lara-hierarchial-collections/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/5a1faa3ed723d9591d47924a6b8a94d04412dd923fc292ab07d5f684db338b81/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f626572746d61726e65792f6c6172612d686965726172636869616c2d636f6c6c656374696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/robertmarney/lara-hierarchial-collections)

Package to extend collections of hierarchical data to organize data into nodes according to the hierarchy.

The package supports unlimited starting nodes, and has been tested to 10 levels deep.

### Use Cases:

[](#use-cases)

- Organizational Charts
- Chart of Accounts

Requirements:
-------------

[](#requirements)

- Illuminate Collections 8+/9+/10+/11+/12+ (Packaged in Laravel 8+/9+/10+/11+/12+)
- PHP 8.0 / 8.1 / 8.2 / 8.3 / 8.4

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

[](#installation)

You can install the package via composer:

```
composer require robertmarney/lara-hierarchial-collections
```

Basic Usage,
------------

[](#basic-usage)

The tool accepts Support Collections or Eloquent Collections, within the collection we expect Eloquent Models or `StdClass` objects.

Assuming a primary key of `id` and parent identifier of `parent_id`:

```
$collection = User::select(['id', 'parent_id', 'name'])->get();

$hierarchy = Hierarchical::make($collection);    // or new Hierarchical($collection);

$result = $hierarchy->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'children' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'children' => [//...]
        ],
        //...
    ]
]
```

### Customizing Local Key:

[](#customizing-local-key)

If you are not using ids (eg uuid) you can override the local comparison value:

```
$hierarchy = new Hierarchical($collection, localIdentifier: 'custom_primary_key')
```

### Customizing Parent Key:

[](#customizing-parent-key)

Similiarly, you can change the parent key if the local relationship is not formed on the default `parent_id`

```
$hierarchy = new Hierarchical($collection, parentIdentifier: 'custom_parent_id')
```

### Providing the `relationName` property will change the collection name where children will be placed

[](#providing-the-relationname-property-will-change-the-collection-name-where-children-will-be-placed)

```
$hierarchy = (new Hierarchical($collection, relationName: 'descendants'))->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'descendants' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'descendants' => [//...]
        ],
        //...
    ]
]
```

### Collection Macro (Laravel Only):

[](#collection-macro-laravel-only)

The package also provides a collection macro to easily convert collections to a hierarchy:

```
$collection = User::select(['id', 'parent_id', 'name'])->get();
$result = $collection->toHierarchical();
```

### Helper Methods:

[](#helper-methods)

#### Ancestors

[](#ancestors)

```
Hierarchical::make($collection)->ancestorsOf($id); // Will resolve all ancestors of the given id

Hierarchical::make($collection)->ancestorsOf($item); // Will resolve all ancestors of the given item
```

#### Descendants

[](#descendants)

```
Hierarchical::make($collection)->descendantsOf($id); // Will resolve all descendants of the given id`

Hierarchical::make($collection)->descendantsOf($item); // Will resolve all descendants of the given item
```

#### Siblings

[](#siblings)

```
Hierarchical::make($collection)->siblingsOf($id); // Will resolve all siblings of the given id

Hierarchical::make($collection)->siblingsOf($item); // Will resolve all siblings of the given item
```

#### Depth

[](#depth)

```
Hierarchical::make($collection)->depthOf($id); // Will resolve the depth of the given id (eg 0, 1, 2, 3, ...)

Hierarchical::make($collection)->depthOf($item); // Will resolve the depth of the given item
```

#### Fluent Comparison

[](#fluent-comparison)

```
Hierarchical::make($collection)->is($id)->siblingOf($id); // boolean

Hierarchical::make($collection)->is($item)->siblingOf($item); // boolean

Hierarchical::make($collection)->is($id)->childOf($id); // boolean

Hierarchical::make($collection)->is($item)->childOf($item); // boolean

Hierarchical::make($collection)->is($id)->ancestorOf($id); // boolean

Hierarchical::make($collection)->is($item)->ancestorOf($item); // boolean
```

### Legacy Usage (Deprecated)

[](#legacy-usage-deprecated)

```
$laraHierarchy = new RCM\LaraHierarchy\LaraHierarchy();

$collection = User::select(['id', 'parent_id', 'name'])->get();

$hierarchy = $laraHierarchy->collectionToHierarchy($collection)->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'children' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'children' => [//...]
        ],
        //...
    ]
]
```

### Customizing Local Key:

[](#customizing-local-key-1)

If you are not using ids (eg uuid) you can override the local comparison value:

```
$hierarchy = $laraHierarchy->collectionToHierarchy($collection, localIdentifier: 'custom_primary_key')
```

### Customizing Parent Key:

[](#customizing-parent-key-1)

Similiarly, you can change the parent key if the local relationship is not formed on the default `parent_id`

```
$hierarchy = $laraHierarchy->collectionToHierarchy($collection, parentIdentifier: 'custom_parent_id')
```

### Providing the `relationName` property will change the collection name where children will be placed

[](#providing-the-relationname-property-will-change-the-collection-name-where-children-will-be-placed-1)

```
$hierarchy = $laraHierarchy->collectionToHierarchy($collection, relationName: 'descendants')->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'descendants' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'descendants' => [//...]
        ],
        //...
    ]
]
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Robert Marney](https://github.com/robertmarney)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance70

Regular maintenance activity

Popularity30

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 56% 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 ~101 days

Recently: every ~171 days

Total

10

Last Release

430d ago

Major Versions

0.1.2 → 1.0.02024-02-06

PHP version history (5 changes)0.0.1PHP ^8.1

0.0.2PHP ^8.0|^8.1

0.1.0PHP ^8.0|^8.1|^8.2

1.0.0PHP ^8.0|^8.1|^8.2|^8.3

1.2.0PHP ^8.0|^8.1|^8.2|^8.3|^8.4

### Community

Maintainers

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

---

Top Contributors

[![robertmarney](https://avatars.githubusercontent.com/u/48888686?v=4)](https://github.com/robertmarney "robertmarney (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (11 commits)")

---

Tags

laravelcollectionschart of accountsrobertmarneylara-hierarchyorg chart

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/robertmarney-lara-hierarchial-collections/health.svg)

```
[![Health](https://phpackages.com/badges/robertmarney-lara-hierarchial-collections/health.svg)](https://phpackages.com/packages/robertmarney-lara-hierarchial-collections)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

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