PHPackages                             sammycorgi/laravel-hierarchy - 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. sammycorgi/laravel-hierarchy

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

sammycorgi/laravel-hierarchy
============================

Single root node hierarchy implementation for Laravel

v1.1.0(4y ago)34MITPHPPHP ^8

Since Aug 19Pushed 4y ago1 watchersCompare

[ Source](https://github.com/sammycorgi/laravel-hierarchy)[ Packagist](https://packagist.org/packages/sammycorgi/laravel-hierarchy)[ RSS](/packages/sammycorgi-laravel-hierarchy/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

laravel-hierarchy
=================

[](#laravel-hierarchy)

Single root node hierarchy implementation for Laravel

Installation
============

[](#installation)

You can install the package via composer:

`composer require sammycorgi/laravel-hierarchy`

Publish the config by running the following console command:

`php artisan vendor:publish --provider="Sammycorgi\LaravelHierarchy\HierarchyServiceProvider" --tag="config"`

If using the `EloquentPersister`, publish the migration for HierarchyModelChildRecord by running the following console command:

`php artisan vendor:publish --provider="Sammycorgi\LaravelHierarchy\HierarchyServiceProvider" --tag="migrations"`

Also make sure to set `hierarchy.eloquent.default_model` in the config to your default `HasHierarchy` Model if you're going to use the `EloquentPersister`

Usage
=====

[](#usage)

Implement the `Contracts\HasHierarchy` interface on any `Model`. For ease, use the `Traits\HasHierarchy` trait to implement the methods automatically. By default, the ID/Parent ID properties are `id` and `parent_id`, respectively. Using this trait, whenever a record is created, deleted or one has its `parent_id` updated, all affected parents will have their persisted child IDs updated automatically (see below).

Inject (or otherwise instantiate) `HierarchyServiceFactory` and pass a `Model` which implements `HasHierarchy` into the `getChildIdsFor()` method to return a collection of IDs which belong to children of the passed model. Call the `for()` method to return a `HierarchyService` with dependencies automatically resolved. You may also pass arguments to this method to override the default dependencies.

Persisting Child IDs
--------------------

[](#persisting-child-ids)

This package makes use of persistence via the cache (by default) or eloquent for quicker fetching of a model's children. If you wish to implement your own persister, create a new class that implements the `Contracts\PersistsHierarchyChildren` interface.

The default persister is the `CachePersister`, but this can be modified in the config. Simply change `hierarchy.persisters.default` in the config to the value of another key in this array, or change the `HIERARCHY_DEFAULT_PERSISTER` in your `.env` file.

Whenever the hierarchy tree is built for a given model, the child IDs will be persisted forever using the `CachePersister`.

When using the `EloquentPersister`, the database will be used to persist these child IDs. New records will be added to the `hierarchy_model_child_records` table that include the model type, the child ID and the parent ID.

Fetching Models
---------------

[](#fetching-models)

By default, this package uses the `EloquentHierarchyInstanceGetter` to fetch `HasHierarchy` models. If you wish to implement your own instance getter, create a new class that implements the `Contracts\GetsHierarchyInstances` interface.

The default instance getter is the `EloquentHierarchyInstanceGetter`, but this can be modified in the config. Simply change `hierarchy.instance_getters.default` in the config to the value of another key in this array.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

1728d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/35593534?v=4)[Sam Driver](/maintainers/sammycorgi)[@sammycorgi](https://github.com/sammycorgi)

---

Top Contributors

[![sammycorgi](https://avatars.githubusercontent.com/u/35593534?v=4)](https://github.com/sammycorgi "sammycorgi (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sammycorgi-laravel-hierarchy/health.svg)

```
[![Health](https://phpackages.com/badges/sammycorgi-laravel-hierarchy/health.svg)](https://phpackages.com/packages/sammycorgi-laravel-hierarchy)
```

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[livewire/volt

An elegantly crafted functional API for Laravel Livewire.

4195.3M84](/packages/livewire-volt)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)

PHPackages © 2026

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