PHPackages                             imanghafoori/eloquent-relativity - 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. imanghafoori/eloquent-relativity

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

imanghafoori/eloquent-relativity
================================

A way to truly decouple eloquent models.

v1.0.10(2y ago)14825.1k13[1 issues](https://github.com/imanghafoori1/eloquent-relativity/issues)MITPHPPHP ^7.1.3|7.2.\*|7.3.\*|7.4.\*|8.0.\*|8.1.\*|8.2.\*|8.3.\*CI failing

Since May 19Pushed 2y ago2 watchersCompare

[ Source](https://github.com/imanghafoori1/eloquent-relativity)[ Packagist](https://packagist.org/packages/imanghafoori/eloquent-relativity)[ Docs](https://github.com/imanghafoori1/laravel-relativity)[ RSS](/packages/imanghafoori-eloquent-relativity/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (3)Versions (13)Used By (0)

Eloquent Relativity
===================

[](#eloquent-relativity)

This allows you to decouple your eloquent models from one another, by defining relations dynamically at run-time.

### - Note that this package is NOT needed in laravel 7.x or above.

[](#--note-that-this-package-is-not-needed-in-laravel-7x-or-above)

Read more:

[![widgetize_header](https://user-images.githubusercontent.com/6961695/57988296-be261180-7aa1-11e9-9e28-645ab0da75dd.png)](https://user-images.githubusercontent.com/6961695/57988296-be261180-7aa1-11e9-9e28-645ab0da75dd.png)

[![Build Status](https://camo.githubusercontent.com/5dac32d4c733114a7d1610eb58065be686214ed8e1e0d66fc96b47b3cbf36c5c/68747470733a2f2f7472617669732d63692e6f72672f696d616e676861666f6f7269312f656c6f7175656e742d72656c617469766974792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/imanghafoori1/eloquent-relativity)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6d82af4143a39cbcd2693b74d9150c648c8155ff57144e3d74547ed8ef0ae47b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696d616e676861666f6f7269312f656c6f7175656e742d72656c617469766974792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/imanghafoori1/eloquent-relativity/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d57e9809aee911ae95962f9789b002f18d67269a76b233866a46a63cdea9390e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696d616e676861666f6f7269312f656c6f7175656e742d72656c617469766974792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/imanghafoori1/eloquent-relativity/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/f440656e0582c46ff28bda591b5caa4a14ac28dde432550a0f9b3816da972a4b/68747470733a2f2f706f7365722e707567782e6f72672f696d616e676861666f6f72692f656c6f7175656e742d72656c617469766974792f762f737461626c65)](https://packagist.org/packages/imanghafoori/eloquent-relativity)[![StyleCI](https://camo.githubusercontent.com/1dbaca8ed314ec098f47dbbf2ace46962d2bd57463975f89b9585db46347aa55/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3138363439363132352f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/186496125)[![Total Downloads](https://camo.githubusercontent.com/61ab3a88717b371d07fe924b74bfd1a55954dd180a79dcde1853a146cdbef96a/68747470733a2f2f706f7365722e707567782e6f72672f696d616e676861666f6f72692f656c6f7175656e742d72656c617469766974792f646f776e6c6f616473)](https://packagist.org/packages/imanghafoori/eloquent-relativity)

#### Compatibility :

[](#compatibility-)

Laravel version 5.5 and above including version 6

#### ▶️ A problem which stops true modularity :

[](#arrow_forward-a-problem-which-stops-true-modularity-)

Let's face it, imagine you have a modular blog application.

Then, you want to add a `commenting` feature to it, so that users can comment on your articles.

In a modular structure, you have 2 modules (`user` module and `blog` module) and you will add a new module for `comments`

#### ▶️ let's analyze dependencies and couplings :

[](#arrow_forward-lets-analyze-dependencies-and-couplings-)

Here the `blog` module "knows" and "depends" upon the `user` module.

But the `user` module should not know or care about the `blog` module. The `blog` is a `plug-in` on the top of the `user` module.

Now we want to add a `comment` module, on the top of `user` and `blog` module.

[![](https://user-images.githubusercontent.com/6961695/57987611-26242a00-7a99-11e9-8c32-67e76b57420f.jpg)](https://user-images.githubusercontent.com/6961695/57987611-26242a00-7a99-11e9-8c32-67e76b57420f.jpg)

#### ▶️ The Right way :

[](#arrow_forward-the-right-way-)

In a truely modular system when you add the `comments`, you should NOT go and touch the code within the `users` or `blog` module. (Remember the `open-closed` principle in `SOLID` ?!)

Imagine you are in a team and each member is working on a seperate module.

`Blog` module is not yours. your team mate is responsible for it and is allowed to code on it.

But when you want to start to define the eloquent relations between `Comment` and `User` and `Article` models, you immediately realize that you have to put code on the eloquent models of other modules to define the inverse of the relationships. Crap !

Look How everything is pointing inward.

If you look at the `User` folder you will have absolutely no footprint of Comment or Article.

We have to touch the code of both `Blog` and `User` module when add a new `comment` module.

For example : You have to open `User.php` and define the

```
public function comments() {
    return $this->hasMany(Comment::class);
}
```

and this is a no no, because it makes an arrow from inside to outside.

So what to do ?!

How can `Comment` be introduced to the system without modifying the other modules ?! (@\_@)

#### ▶️ Install: (the most painful step)

[](#arrow_forward-install-the-most-painful-step)

```
composer require imanghafoori/eloquent-relativity   (and take a coffee...)

```

Now the installtion finished, you first have to make your models "relative" !!!

By using the `Imanghafoori\Relativity\DynamicRelations` traits on your eloquent models.

[![image](https://user-images.githubusercontent.com/6961695/58089939-465c0200-7bdb-11e9-8df0-2dc5212ced43.png)](https://user-images.githubusercontent.com/6961695/58089939-465c0200-7bdb-11e9-8df0-2dc5212ced43.png)

So the `User`, `Article`, `Comment` will have to have this trait one them.

Now comes the sweet part :

within the `CommentsServiceProvider.php`

```
class CommentsServiceProvider
{
    public function register () {

        User::has_many('comments', Comment::class);     // instead of defining method on the User class.
        Article::has_many('comments',  Comment::class);

        Comment::belongs_to('author', User::class);       // inverse of relations
        Comment::belongs_to('article',  Article::class);
    }

}
```

Now you can do these queries :

```
User::find(1)->comments;
or
User::find(1)->comments()->count();
```

So instead of going to `User` model and define a method there...

```
public function comments() {
    return $this->hasMany(Comment::class);
}
```

You have defined the method remotely from your new module at run-time:

```
User::has_many('comments', Comment::class);
```

Here is a list of supported relations :

- has\_many
- has\_one
- belongs\_to
- belongs\_to\_many
- morph\_to\_many
- morph\_many
- morph\_one
- morph\_to
- morphed\_by\_many
- has\_many\_through

They accept the same paramters as the eloquent equivalent counter part. except the first argument should be relation name.

#### ▶️ Extra features :

[](#arrow_forward-extra-features-)

sometimes you need to call extra methods on the relations.

```
User::has_many('comments', Comment::class)->orderBy('id', 'asc');
```

All the methods are available to you.

- Enforce eager-loading

On reqular eloquent models you may define the

```
User extends Model {
    protected $with = ['comments'];
}
```

instead you can :

```
User::forceEagerLoading('comments');
```

remember this should be in the `boot` method of your Service Provider not the `register` method.

### ⭐ Your Stars Make Us Do More ⭐

[](#star-your-stars-make-us-do-more-star)

As always if you found this package useful and you want to encourage us to maintain and work on it, Please press the star button to declare your willing.

### ▶️ More from the author:

[](#arrow_forward-more-from-the-author)

### Laravel Terminator

[](#laravel-terminator)

💎 A minimal yet powerful package to give you opportunity to refactor your controllers.

-

---

### Laravel Widgetize

[](#laravel-widgetize)

💎 A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.

-

---

### Laravel Master Pass

[](#laravel-master-pass)

💎 A simple package that lets you easily impersonate your users.

-

---

### Laravel HeyMan

[](#laravel-heyman)

💎 It allows to write exressive and defensive code which is decoupled from the rest of your app.

-

---

### 🍌 Reward me a banana 🍌

[](#-reward-me-a-banana-)

so that I will have energy to start the next package for you.

Dodge Coin: DJEZr6GJ4Vx37LGF3zSng711AFZzmJTouN

LiteCoin: ltc1q82gnjkend684c5hvprg95fnja0ktjdfrhcu4c4

BitCoin: bc1q53dys3jkv0h4vhl88yqhqzyujvk35x8wad7uf9

Ripple: rJwrb2v1TR6rAHRWwcYvNZxjDN2bYpYXhZ

Etherium: 0xa4898246820bbC8f677A97C2B73e6DBB9510151e

---

```
Life is like riding a bicycle. To keep your balance you must keep moving.

"Albert Einstein"

```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 93.1% 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 ~141 days

Recently: every ~316 days

Total

12

Last Release

994d ago

Major Versions

v0.3.0 → v1.0.02019-05-21

PHP version history (4 changes)v0.2.0PHP &gt;=7.1.3

v1.0.6PHP ^7.1.3|7.2.\*|7.3.\*|7.4.\*

v1.0.9PHP ^7.1.3|7.2.\*|7.3.\*|7.4.\*|8.\*|8.1.\*

v1.0.10PHP ^7.1.3|7.2.\*|7.3.\*|7.4.\*|8.0.\*|8.1.\*|8.2.\*|8.3.\*

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6961695?v=4)[Iman](/maintainers/imanghafoori1)[@imanghafoori1](https://github.com/imanghafoori1)

---

Top Contributors

[![imanghafoori1](https://avatars.githubusercontent.com/u/6961695?v=4)](https://github.com/imanghafoori1 "imanghafoori1 (81 commits)")[![railken](https://avatars.githubusercontent.com/u/26530231?v=4)](https://github.com/railken "railken (2 commits)")[![amirsadeghi1](https://avatars.githubusercontent.com/u/26359326?v=4)](https://github.com/amirsadeghi1 "amirsadeghi1 (1 commits)")[![asilvestroni](https://avatars.githubusercontent.com/u/4268527?v=4)](https://github.com/asilvestroni "asilvestroni (1 commits)")[![bumbummen99](https://avatars.githubusercontent.com/u/4533331?v=4)](https://github.com/bumbummen99 "bumbummen99 (1 commits)")[![MostafaNorzade](https://avatars.githubusercontent.com/u/24518877?v=4)](https://github.com/MostafaNorzade "MostafaNorzade (1 commits)")

---

Tags

eloquenteloquent-modelslaravel-packagemodularityphplaravellaravel5laravel-eloquent

### Embed Badge

![Health badge](/badges/imanghafoori-eloquent-relativity/health.svg)

```
[![Health](https://phpackages.com/badges/imanghafoori-eloquent-relativity/health.svg)](https://phpackages.com/packages/imanghafoori-eloquent-relativity)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[ymigval/laravel-model-cache

Laravel package for caching Eloquent model queries

7642.2k3](/packages/ymigval-laravel-model-cache)[umbrellio/laravel-ltree

Extension LTree (Postgres) for Laravel

34111.6k](/packages/umbrellio-laravel-ltree)[wayofdev/laravel-cycle-orm-adapter

🔥 A Laravel adapter for CycleORM, providing seamless integration of the Cycle DataMapper ORM for advanced database handling and object mapping in PHP applications.

3516.7k3](/packages/wayofdev-laravel-cycle-orm-adapter)[salehhashemi/laravel-repository

Implementing the repository pattern for Laravel projects.

2010.5k](/packages/salehhashemi-laravel-repository)

PHPackages © 2026

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