PHPackages                             dd-developments/laravel-dynamic-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. dd-developments/laravel-dynamic-relations

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

dd-developments/laravel-dynamic-relations
=========================================

Dynamic, declarative Eloquent relations via maps + resolveRelationUsing.

v1.0.1(6mo ago)114MITPHPPHP ^8.3CI passing

Since Nov 2Pushed 6mo agoCompare

[ Source](https://github.com/dd-developments/laravel-dynamic-relations)[ Packagist](https://packagist.org/packages/dd-developments/laravel-dynamic-relations)[ Fund](https://dd-development.com)[ GitHub Sponsors](https://github.com/dd-developments)[ RSS](/packages/dd-developments-laravel-dynamic-relations/feed)WikiDiscussions main Synced 1mo ago

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

 [ ![dd-developments Hawk Logo](.github/assets/logo.png) ](https://dd-developments.com)

🧩 Laravel Dynamic Relations
===========================

[](#-laravel-dynamic-relations)

 **Dynamic, declarative &amp; trait-driven Eloquent relations for modern Laravel apps**
 by [dd-developments](https://dd-development.com) — Hosted in Belgium 🇧🇪

 [ ![Version](https://camo.githubusercontent.com/cb15ae5b785691757351ef997b9f62243781e198505d5940b6970827172b0f6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64642d646576656c6f706d656e74732f6c61726176656c2d64796e616d69632d72656c6174696f6e732e7376673f7374796c653d666c61742d737175617265) ](https://packagist.org/packages/dd-developments/laravel-dynamic-relations) [![Tests](https://camo.githubusercontent.com/10762529c95fc8e2c589cfb41972f02e85ecbc388d894edfb9f6c59d9725b45f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f64642d646576656c6f706d656e74732f6c61726176656c2d64796e616d69632d72656c6174696f6e732f746573742e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/10762529c95fc8e2c589cfb41972f02e85ecbc388d894edfb9f6c59d9725b45f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f64642d646576656c6f706d656e74732f6c61726176656c2d64796e616d69632d72656c6174696f6e732f746573742e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265) [![Downloads](https://camo.githubusercontent.com/0b8b3b3e750bb833f2a0d59cb1385bb3fb3ecbffd15d41962918681ac1a61234/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64642d646576656c6f706d656e74732f6c61726176656c2d64796e616d69632d72656c6174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0b8b3b3e750bb833f2a0d59cb1385bb3fb3ecbffd15d41962918681ac1a61234/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64642d646576656c6f706d656e74732f6c61726176656c2d64796e616d69632d72656c6174696f6e732e7376673f7374796c653d666c61742d737175617265) [![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265) [![Laravel 12](https://camo.githubusercontent.com/09a63b4b6a6882c97735030d7868e02b43ad6dce8893b1db7ac0829df1a682e9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322e782d6666326432303f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c)](https://camo.githubusercontent.com/09a63b4b6a6882c97735030d7868e02b43ad6dce8893b1db7ac0829df1a682e9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322e782d6666326432303f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c) [![PHP 8.3+](https://camo.githubusercontent.com/77c18db161a57c7ec384c4cc21e51cbb52c3e70b5651891ea221173ad1c9f18f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e332532422d3737376262343f7374796c653d666c61742d737175617265266c6f676f3d706870)](https://camo.githubusercontent.com/77c18db161a57c7ec384c4cc21e51cbb52c3e70b5651891ea221173ad1c9f18f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e332532422d3737376262343f7374796c653d666c61742d737175617265266c6f676f3d706870)

---

💡 What is Laravel Dynamic Relations?
------------------------------------

[](#-what-is-laravel-dynamic-relations)

**Laravel Dynamic Relations** brings a *next-level modularity* to your Eloquent models.
Instead of hardcoding relationships like `hasMany()` or `belongsTo()`, you can **define, register and resolve them dynamically** — via traits or config — keeping your models lightweight and flexible.

Think of it as *“Eloquent relationships, but hot-swappable.”*
Every model can declare its links without being tightly coupled to others.

---

✨ Key Features
--------------

[](#-key-features)

✅ **Trait-first design** – Attach reusable relationship traits to any model
⚙️ **Declarative configuration** – Centralize mappings via `config/dynamic-relations.php`
🧬 **Runtime registration** – Define relations on-the-fly with `DynamicRelations::for()`
🧩 **All relation types supported** – `hasOne`, `hasMany`, `belongsTo`, `belongsToMany`, and all morphs
🚀 **Zero boilerplate** – Auto-registered via the Service Provider
🧱 **Modular &amp; package-friendly** – Ideal for microservice or multi-module Laravel setups
💥 **“Everything is hot-swappable” philosophy** – Replace, extend, or override any relation dynamically

---

⚙️ Installation
---------------

[](#️-installation)

```
composer require dd-developments/laravel-dynamic-relations
php artisan vendor:publish --tag=dynamic-relations-config

That’s it. The service provider automatically hooks into Laravel’s boot cycle and enables runtime mapping.
🧬 Example — Trait-Based Relation

Add reusable relationship logic to any model using traits.

use DdDevelopments\DynamicRelations\Facades\DynamicRelations;

trait HasManyImages
{
    protected static function bootHasManyImages(): void
    {
        DynamicRelations::for(static::class, 'images', fn ($model) =>
            $model->morphMany(Image::class, 'imageable'));
    }
}

Then in your model:

use App\Models\Traits\HasManyImages;

class Post extends Model
{
    use HasManyImages;
}

That’s it — no need to touch your Post model’s body again.
Each relation lives in its own modular trait and can be reused across the ecosystem.
🧠 Why this Package Exists

Laravel’s Eloquent ORM is powerful, but static.
Relationships are normally baked directly into models, making code harder to reuse across modules or packages.

Laravel Dynamic Relations fixes that by providing a declarative API and a runtime registration layer, merging the best of both worlds:
config-driven flexibility and trait-based simplicity.
Feature	This Package	Typical Alternatives
Trait-first design	✅	❌
Config-driven maps	✅	⚠️ Partial
Runtime registration (DynamicRelations::for)	✅	❌
Full morph coverage	✅	⚠️
Laravel 12 + Pest v4 support	✅	⚠️
Hot-swappable relations	✅	❌
🧩 Example Config (Optional)

Prefer central control? You can declare all relations from config/dynamic-relations.php:

return [
    App\Models\Post::class => [
        'author' => fn ($m) => $m->belongsTo(App\Models\User::class, 'user_id'),
        'images' => fn ($m) => $m->morphMany(App\Models\Image::class, 'imageable'),
    ],
];

Every time Laravel boots, these are automatically registered — no manual wiring required.
🧰 Ideal Use Cases

    🧩 Modular CMS architectures

    🧱 Multi-tenant or multi-package Laravel setups

    🧪 Package development (shared traits between domains)

    🔁 Dynamic content models (relations change based on config)

    ⚡ Projects embracing “Everything is hot-swappable” philosophy

🧪 Testing Support

This package ships ready for Pest v4 and Orchestra Testbench.
Simply run:

composer test

and you’re good to go.
📜 License

Released under the MIT License
© 2025 dd-developments.com

— All rights reserved.
🧠 Author

Built with 💡 by Daniel Demesmaecker
for dd-development.com

— Hosted in Belgium 🇧🇪

    Everything is hot-swappable.
    Built for modular CMS architectures where every relation is replaceable, extendable, and reusable.

                                          Crafted with precision by dd-development • Innovation through modularity.
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance68

Regular maintenance activity

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

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

6

Last Release

188d ago

Major Versions

v0.2.0 → v1.0.02025-11-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/92ad8c2fbe5349226f5d818c2f5402773abc90f6c7d7940a28b0c86a944cf2cc?d=identicon)[dd-developments](/maintainers/dd-developments)

---

Top Contributors

[![dd-developments](https://avatars.githubusercontent.com/u/241507676?v=4)](https://github.com/dd-developments "dd-developments (51 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/dd-developments-laravel-dynamic-relations/health.svg)

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

###  Alternatives

[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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