Transform flat collection to a nested Hierarchy
MIT License
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.
You can install the package via composer:
composer require robertmarney/lara-hierarchial-collections
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' => [//...]
],
//...
]
]
If you are not using ids (eg uuid) you can override the local comparison value:
$hierarchy = new Hierarchical($collection, localIdentifier: 'custom_primary_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')
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' => [//...]
],
//...
]
]
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();
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
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
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
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
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
$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' => [//...]
],
//...
]
]
If you are not using ids (eg uuid) you can override the local comparison value:
$hierarchy = $laraHierarchy->collectionToHierarchy($collection, localIdentifier: 'custom_primary_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')
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' => [//...]
],
//...
]
]
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.