PHPackages                             paxha/laravel-recursive-relationships - 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. paxha/laravel-recursive-relationships

ActiveLaravel

paxha/laravel-recursive-relationships
=====================================

This Laravel Eloquent extension provides recursive relationships using common table.

v1.1.0(5y ago)2752.5k↓42.9%5[1 issues](https://github.com/paxha/laravel-recursive-relationships/issues)1MITPHPPHP ^7.2|^8.0

Since Dec 14Pushed 5y ago1 watchersCompare

[ Source](https://github.com/paxha/laravel-recursive-relationships)[ Packagist](https://packagist.org/packages/paxha/laravel-recursive-relationships)[ RSS](/packages/paxha-laravel-recursive-relationships/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (1)

[![Build Status](https://camo.githubusercontent.com/5cf55c2d7add9957f1bb36759a6acb2a014917e155d85623e6cf856d80917fb3/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f70617868612f6c61726176656c2d7265637572736976652d72656c6174696f6e73686970732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/paxha/laravel-recursive-relationships)[![StyleCI](https://camo.githubusercontent.com/ffd094ae1665e0805e42d1fe0bb258fd89a45196a007354c932005d470cfea49/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3232373038363739372f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/227086797)[![Total Downloads](https://camo.githubusercontent.com/dcb7545c6e54a54c296dfb6f0241ed6f99d226d9ba77d15fa439425d7508f53e/68747470733a2f2f706f7365722e707567782e6f72672f70617868612f6c61726176656c2d7265637572736976652d72656c6174696f6e73686970732f642f746f74616c2e7376673f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/paxha/laravel-recursive-relationships)[![Latest Stable Version](https://camo.githubusercontent.com/4dceeae9903bf751a62562220559015f25f56cf753f62eea37bcab23a7f4fe70/68747470733a2f2f706f7365722e707567782e6f72672f70617868612f6c61726176656c2d7265637572736976652d72656c6174696f6e73686970732f762f737461626c652e7376673f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/paxha/laravel-recursive-relationships)[![License](https://camo.githubusercontent.com/2b5f95f6e863ca357f75dbf4f296dfd06315bd53f0f941ea41779cdc8bf0a5d8/68747470733a2f2f706f7365722e707567782e6f72672f70617868612f6c61726176656c2d7265637572736976652d72656c6174696f6e73686970732f6c6963656e73652e7376673f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/paxha/laravel-recursive-relationships)

Introduction
------------

[](#introduction)

This Laravel Eloquent extension provides recursive relationships using common table.

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

[](#installation)

```
composer require paxha/laravel-recursive-relationships

```

Usage
-----

[](#usage)

- [Getting Started](#getting-started)
- [Relationships](#relationships)
- [Scopes](#scopes)
- [Functions](#functions)

### Getting Started

[](#getting-started)

Consider the following table schema for hierarchical data:

```
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('parent_id')->nullable();
});
```

Use the `HasRecursiveRelationships` trait in your model to work with recursive relationships:

```
class User extends Model
{
    use \RecursiveRelationships\Traits\HasRecursiveRelationships;
}
```

By default, the trait expects a parent key named `parent_id`. You can customize it by overriding `getParentKeyName()`:

```
class User extends Model
{
    use \RecursiveRelationships\Traits\HasRecursiveRelationships;

    public function getParentKeyName()
    {
        return 'user_id'; // or anything
    }
}
```

### Relationships

[](#relationships)

The trait provides various relationships:

- `children()`: The model's direct children.
- `nestedChildren()`: The model's nested children.
- `parent()`: The model's direct parent.
- `nestedParents()`: The model's nested parents by object.

```
$users = User::with('children')->get();

$users = User::with('nestedChildren')->get();

$users = User::with('parent')->get();

$users = User::with('nestedParents')->get();
```

### Scopes

[](#scopes)

The trait provides query scopes to filter models by their position in the tree:

- `hasChildren()`: Models with children.
- `hasParent()`: Models with a parent.
- `leaf()`: Models without children.
- `root()`: Models without a parent.

```
$noLeaves = User::hasChildren()->get();

$noRoots = User::hasParent()->get();

$leaves = User::leaf()->get();

$roots = User::root()->get();
```

### Functions

[](#functions)

The trait provides helper functions:

- `descendents()`: The model's all Children in single array.
- `ancestors()`: The model's all parents in single array.
- `siblings()`: The parent's other children.

```
$descendents = User::find($id)->descendents();

$ancestors = User::find($id)->ancestors();

$siblings = User::find($id)->siblings();
```

License
-------

[](#license)

This is open-sourced laravel library licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community12

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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 ~184 days

Total

3

Last Release

1978d ago

PHP version history (2 changes)v1.0PHP ^7.2

v1.1.0PHP ^7.2|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/2816953f287ca302f12d59f94de4e90cd6cb5d2ba1f6eaa462045c2670969ea0?d=identicon)[paxha](/maintainers/paxha)

---

Top Contributors

[![paxha](https://avatars.githubusercontent.com/u/38579192?v=4)](https://github.com/paxha "paxha (39 commits)")

---

Tags

laravellaravel-eloquent-extensionlaravel-recusive-relationshipsrecursive-relationshipstraitlaravellaravel-librarylaravel recursive relationshipsrecursive relationshipsparent child relations

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/paxha-laravel-recursive-relationships/health.svg)

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

###  Alternatives

[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[hemp/presenter

Easy Model Presenters in Laravel

247592.6k1](/packages/hemp-presenter)[slowlyo/owl-admin

基于 laravel、amis 开发的后台框架~

61214.2k26](/packages/slowlyo-owl-admin)[binafy/laravel-reactions

React to something in Laravel framework

874.5k](/packages/binafy-laravel-reactions)[shipsaas/laravel-priority-queue

Priority Queue implementation for your Laravel Applications

492.8k](/packages/shipsaas-laravel-priority-queue)[shipsaas/laravel-inbox-process

Inbox pattern process implementation for your Laravel Applications

132.1k](/packages/shipsaas-laravel-inbox-process)

PHPackages © 2026

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