PHPackages                             michaelorenda/laravel-recursive-relations - 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. michaelorenda/laravel-recursive-relations

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

michaelorenda/laravel-recursive-relations
=========================================

Recursive parent-child traversal for Eloquent models.

v1.0.3(5mo ago)02MITPHPPHP ^8.2

Since Nov 29Pushed 5mo agoCompare

[ Source](https://github.com/michael-orenda/laravel-recursive-relations)[ Packagist](https://packagist.org/packages/michaelorenda/laravel-recursive-relations)[ Docs](https://github.com/michaelorenda/laravel-recursive-relations)[ RSS](/packages/michaelorenda-laravel-recursive-relations/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

Laravel Recursive Relations
===========================

[](#laravel-recursive-relations)

Powerful recursive parent–child relationships for Laravel **12+**

A lightweight, framework-native package for building hierarchical data structures such as:

- Organization Units
- Categories &amp; Subcategories
- File Trees
- Product &amp; Menu Structures
- Multi-level Comment Threads
- Any recursive / tree-based domain

---

🚀 Features
----------

[](#-features)

- **Pure nested tree output** via `tree()`
- **Flat descendant lists** via `descendants()`
- **Unlimited or depth-limited recursion**
- **Guaranteed no repetition / no duplicated nodes**
- **Automatic ancestry resolution** (`ancestors()`, `root()`)
- Optional caching layer via `HasRecursiveCache`
- Fully compatible with **Laravel 12+**
- Zero dependencies; Eloquent-powered
- Works with any model

---

📦 Installation
--------------

[](#-installation)

```
composer require michaelorenda/laravel-recursive-relations
```

Laravel will auto-discover the service provider.

---

🧩 Usage Example
---------------

[](#-usage-example)

### 1. Add the Trait

[](#1-add-the-trait)

```
use MichaelOrenda\LaravelRecursiveRelations\Traits\HasRecursiveRelations;

class Category extends Model
{
    use HasRecursiveRelations;

    protected $fillable = ['name', 'parent_id'];
}
```

### 2. Required Migration

[](#2-required-migration)

```
$table->unsignedBigInteger('parent_id')->nullable()->index();
```

---

🌳 Building Trees
----------------

[](#-building-trees)

### Pure Nested Tree

[](#pure-nested-tree)

```
$tree = Category::find(1)->tree();
```

Produces:

```
[
  {
    "id": 2,
    "name": "Child",
    "children": [
      { "id": 7, "name": "Grandchild", "children": [] }
    ]
  }
]
```

✔️ No duplication
✔️ No repeated branches
✔️ Perfect hierarchy

### Flat Descendants

[](#flat-descendants)

```
$flat = Category::find(1)->descendants();
```

---

🔗 Ancestry Tools
----------------

[](#-ancestry-tools)

```
$node->parent;
$node->children;
$node->ancestors();
$node->root();
```

---

⚙️ Customizing Keys
-------------------

[](#️-customizing-keys)

```
protected static function recursiveConfig(): array
{
    return [
        'parent_key' => 'parent_unit_id',
        'local_key'  => 'unit_id',
    ];
}
```

---

📚 API Documentation
-------------------

[](#-api-documentation)

See [`API_DOCS.md`](API_DOCS.md)

---

🔐 Security
----------

[](#-security)

See [`SECURITY.md`](SECURITY.md)

---

🤝 Contributing
--------------

[](#-contributing)

PRs welcome! Follow PSR-12 and include tests.

---

📄 License
---------

[](#-license)

MIT — Free for personal and commercial use.

---

*Last updated: 2025-11-29*

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance71

Regular maintenance activity

Popularity2

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

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

4

Last Release

164d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/717e442360b4b2518bad07d4b6942676841ef358236c52c8d5f5929b3d908eaa?d=identicon)[rminchrist](/maintainers/rminchrist)

---

Tags

laravelnestedeloquenttreehierarchyrelationsrecursiveparent-child

### Embed Badge

![Health badge](/badges/michaelorenda-laravel-recursive-relations/health.svg)

```
[![Health](https://phpackages.com/badges/michaelorenda-laravel-recursive-relations/health.svg)](https://phpackages.com/packages/michaelorenda-laravel-recursive-relations)
```

###  Alternatives

[baril/bonsai

An implementation of the Closure Tables pattern for Eloquent.

3593.5k](/packages/baril-bonsai)[typicms/nestablecollection

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

88327.2k20](/packages/typicms-nestablecollection)[nevadskiy/laravel-tree

Tree-like structure for Eloquent models.

6693.9k4](/packages/nevadskiy-laravel-tree)[jiaxincui/closure-table

A closure table tree package for laravel eloquent

1138.4k](/packages/jiaxincui-closure-table)[alibayat/laravel-categorizable

Implementing Nested-structure Hierarchy Categories system for Eloquent models.

602.7k](/packages/alibayat-laravel-categorizable)

PHPackages © 2026

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