PHPackages                             stayallive/laravel-inverse-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. stayallive/laravel-inverse-relations

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

stayallive/laravel-inverse-relations
====================================

HasMany, HasOne and MorphMany Eloquent relations with inverse hydration.

v1.1.0(2y ago)3011.0k↓50%2MITPHP

Since Apr 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/stayallive/laravel-inverse-relations)[ Packagist](https://packagist.org/packages/stayallive/laravel-inverse-relations)[ Docs](https://github.com/stayallive/laravel-inverse-relations)[ GitHub Sponsors](https://github.com/stayallive)[ RSS](/packages/stayallive-laravel-inverse-relations/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

Laravel Inverse Relations
=========================

[](#laravel-inverse-relations)

[![Latest Version](https://camo.githubusercontent.com/e949ae9ed7ee381ed76754bdb1a6112e8dae5b6d2d38c44161d8c53e33b8c6d0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73746179616c6c6976652f6c61726176656c2d696e76657273652d72656c6174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://github.com/stayallive/laravel-inverse-relations/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/0fb89d53301921cde361f39cc53e6efc405a9534e4ff9db817c012a44e4be5f8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73746179616c6c6976652f6c61726176656c2d696e76657273652d72656c6174696f6e732f63692e79616d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/stayallive/laravel-inverse-relations/actions/workflows/ci.yaml)[![Total Downloads](https://camo.githubusercontent.com/d87e256eb15032c863b2c727e9a2a041442cfd7bde305f446241a17ddcf10c5b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746179616c6c6976652f6c61726176656c2d696e76657273652d72656c6174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stayallive/laravel-inverse-relations)

`HasMany`, `HasOne` and `MorphMany` Eloquent relations with inverse hydration.

[Jonathan Reinink](https://github.com/reinink) wrote a great blog post about [optimizing circular relationships in Laravel](https://reinink.ca/articles/optimizing-circular-relationships-in-laravel). The package [stancl/laravel-hasmanywithinverse](https://github.com/archtechx/laravel-hasmanywithinverse) ran with that idea and implemented it for the `hasMany` relation. This package improves on that by implementing it for the `hasOne` and `morphMany` relations too.

In short, this package allows you to define inverse relations on your models. This means that you can define a relation on a model that points to another model. This is useful when you have a circular relationship between two models and you want to be able to access the inverse relation without having to load the other model from the database potentially saving a lot of database queries.

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

[](#installation)

```
composer require stayallive/laravel-inverse-relations
```

Usage
-----

[](#usage)

Adding the `HasHasManyWithInverseRelation`, `HasHasOneWithInverseRelation` and/or `HasMorphManyWithInverseRelation` traits will provide you with the helper methods to setup the inverse relations.

Check out the test stubs for examples on how to implement the traits on your models to setup the inverse relations:

- [HasMany](tests/Stubs/HasManyWithInverse)
- [HasOne](tests/Stubs/HasOneWithInverse)
- [MorphMany](tests/Stubs/MorphManyWithInverse)

Caveats
-------

[](#caveats)

Because the inverse relation stores the parent by reference (not cloned), you need to be careful when you are using the relation since you will be modifying the parent model. This is nothing new to this package since eager loading does the same thing but I thought to mention it since it could catch you off guard.

There is one other downside to the inverse relation being stored as a reference since it creates a circular reference. This means that you cannot recusively serialize the model that has the inverse relation. Laravel does this for example when you pass the model in a queued job since the `SerializesModels` trait will also try to store which relations were loaded when the model was queued to load those relations back causing once the job is being processed. This causes an infinite loop trying to figure out which relations are loaded recursively, eating up memory until the process is killed by the OS or crashes.

To prevent this you can either pass your model to your job using `$model->withoutRelations()` or you can disable the relations loading on your model by adding the following method to your model:

```
    public function getQueueableRelations(): array
    {
        return [];
    }
```

This disables the collection and loading of relations on the model when it (un-)serialized for the queued job.

Personally I put this in my base model since I never want to load the relations loaded when I push a job on the queue since they are mostly not useful in the context of the job and apart from guarding against circular references this also saves database queries (sometimes a lot) retrieving unused relations.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

If you discover a security vulnerability within this package, please send an e-mail to Alex Bouma at `alex+security@bouma.me`. All security vulnerabilities will be swiftly addressed.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90% 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 ~127 days

Total

4

Last Release

751d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1090754?v=4)[Alex Bouma](/maintainers/stayallive)[@stayallive](https://github.com/stayallive)

---

Top Contributors

[![stayallive](https://avatars.githubusercontent.com/u/1090754?v=4)](https://github.com/stayallive "stayallive (9 commits)")[![IonBazan](https://avatars.githubusercontent.com/u/1985514?v=4)](https://github.com/IonBazan "IonBazan (1 commits)")

---

Tags

eloquentlaravel-packagelaraveleloquentrelations

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/stayallive-laravel-inverse-relations/health.svg)

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

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[bavix/laravel-clickhouse

Eloquent model for ClickHouse

72214.1k2](/packages/bavix-laravel-clickhouse)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

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