robertmarney/lara-hierarchial-collections

Transforms flat collections to a nested hierarchy
2,585 34
Install
composer require robertmarney/lara-hierarchial-collections
Latest Version:1.2.0
PHP:^8.0|^8.1|^8.2|^8.3|^8.4
Laravel:8 – 12
License:MIT
Last Updated:Oct 15, 2026
Links: GitHub  ·  Packagist
Maintainer: robertmarney

Transforms flat collections to a nested hierarchy

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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:

  • Organizational Charts
  • Chart of Accounts

Requirements:

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

Installation

You can install the package via composer:

composer require robertmarney/lara-hierarchial-collections

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:

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:

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

$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):

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:

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

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

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
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

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)

$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:

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:

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

$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

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

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

Related Packages

mookofe/laravel-support

Awesome enhancements for your current Laravel Models, Collections and more.

12 3
eznio/db

DB Collections library inspired by Laravel

43 0
collector/collector

A collection library inspired by Laravel Collections, built using generators to...

208 15
mb4it/collections

Separate collections Illuminate\Support\Collections

394 0
spatie/piper

A pipe operator-first PHP utility library for array and string manipulation

3,074 57

Version History

Version Released PHP Laravel License
1.2.0 ^8.0|^8.1|^8.2|^8.3|^8.4 ^8.0||^9.0||^10.0||^11.0||^12.0 MIT
1.1.0 ^8.0|^8.1|^8.2|^8.3 ^8.0|^9.0|^10.0|^11.0 MIT
1.0.0 ^8.0|^8.1|^8.2|^8.3 ^8.0|^9.0|^10.0|^11.0 MIT
0.1.2 ^8.0|^8.1|^8.2 ^8.0|^9.0|^10.0 MIT
0.1.1 ^8.0|^8.1|^8.2 ^8.0|^9.0|^10.0 MIT