PHPackages                             nevadskiy/laravel-many-to-morph - 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. nevadskiy/laravel-many-to-morph

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

nevadskiy/laravel-many-to-morph
===============================

The missing polymorphic relationship for Eloquent.

0.2.0(2y ago)51011[2 PRs](https://github.com/nevadskiy/laravel-many-to-morph/pulls)MITPHPPHP ^7.4 || ^8.0.2

Since Oct 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/nevadskiy/laravel-many-to-morph)[ Packagist](https://packagist.org/packages/nevadskiy/laravel-many-to-morph)[ RSS](/packages/nevadskiy-laravel-many-to-morph/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

The Many-To-Morph relationship for Eloquent
===========================================

[](#the-many-to-morph-relationship-for-eloquent)

A package that simplifies and enhances Many-To-Many polymorphic relationships in Laravel's Eloquent ORM.

[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://stand-with-ukraine.pp.ua)

[![PHPUnit](https://camo.githubusercontent.com/530589214576c141efe8d4964246852dde3ddbf94308f3f6e68cdff847e08906/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e65766164736b69792f6c61726176656c2d6d616e792d746f2d6d6f7270682f706870756e69742e796d6c3f6272616e63683d6d6173746572)](https://packagist.org/packages/nevadskiy/laravel-many-to-morph)[![Code Coverage](https://camo.githubusercontent.com/c0beb0c9ccde8f626862367d36d3b4c1f13208fa9e0ae6875b77da26deef3b00/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6e65766164736b69792f6c61726176656c2d6d616e792d746f2d6d6f7270683f746f6b656e3d39583641515159435041)](https://packagist.org/packages/nevadskiy/laravel-many-to-morph)[![Latest Stable Version](https://camo.githubusercontent.com/f3aa7e1e485f312a8d2dd34f59f4587293e8cc3f82e3e717c3aeee2ab6b4351c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e65766164736b69792f6c61726176656c2d6d616e792d746f2d6d6f727068)](https://packagist.org/packages/nevadskiy/laravel-many-to-morph)[![License](https://camo.githubusercontent.com/a0e8b208025107052b8b329878592abf625459f6b6391b5a8ed78d3baffcaa62/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6e65766164736b69792f6c61726176656c2d6d616e792d746f2d6d6f727068)](https://packagist.org/packages/nevadskiy/laravel-many-to-morph)

📚 Introduction
--------------

[](#-introduction)

One common type of relationship in Laravel's Eloquent ORM is the [Many-To-Many polymorphic relation](https://laravel.com/docs/10.x/eloquent-relationships#many-to-many-polymorphic-relations). While it works well for most use cases, you might encounter certain challenges that require more elegant solutions, for example:

1. When you have numerous related models, you need to define a separate relation for each type of model.
2. It is hard to retrieve all related models at once.

This package introduces a new Many-To-Morph relationship, inspired by the Directus's [Many-to-Any relation](https://docs.directus.io/app/data-model/relationships.html#many-to-any-m2a) that handles these problems.

📝 Table of Contents
-------------------

[](#-table-of-contents)

- [Installation](#-installation)
- [Documentation](#-documentation)
    - [Configuring Relationship](#configuring-relationship)
    - [Retrieving Relationships](#retrieving-relationships)
    - [Ordering Relationships](#ordering-relationships)
    - [Eager Loading Relationships](#eager-loading-relationships)
    - [Attaching Relationships](#attaching-relationships)
    - [Detaching Relationships](#detaching-relationships)
- [License](#-license)

🔌 Installation
--------------

[](#-installation)

Install the package via Composer:

```
composer require nevadskiy/laravel-many-to-morph
```

📄 Documentation
---------------

[](#-documentation)

### Configuring Relationship

[](#configuring-relationship)

To configure this relationship, you need to use the `HasManyToMorph` trait, which provides a `manyToMorph` method for defining the relation as follows:

```
use Nevadskiy\ManyToMorph\HasManyToMorph;
use Nevadskiy\ManyToMorph\ManyToMorph;

class Tag extends Model
{
    use HasManyToMorph;

    public function taggables(): ManyToMorph
    {
        return $this->manyToMorph('taggable');
    }
}
```

### Retrieving Relationships

[](#retrieving-relationships)

You can retrieve relationships as shown below:

```
use App\Models\Tag;
use App\Models\Post;
use App\Models\Video;

$tag = Tag::find(1);

foreach ($tag->taggables as $taggable) {
    if ($taggable instanceof Post) {
        // ...
    } else if ($taggable instanceof Video) {
        // ...
    }
}
```

### Ordering Relationships

[](#ordering-relationships)

To order relationships, you can use the `orderBy` method directly on the `taggables` relation like so:

```
use App\Models\Tag;

$tag = Tag::find(1);

$tag->taggables()->orderBy('position')->get();
```

### Eager Loading Relationships

[](#eager-loading-relationships)

Eager loading relationships can be done like this:

```
use App\Models\Tag;
use App\Models\Post;
use App\Models\Video;

$tags = Tag::query()
    ->with(['taggables' => function (ManyToMorph $taggables) {
        $taggables->morphWith([
            Post::class => ['media'],
            Video::class => ['previews'],
        ]);
    }])
    ->get();
```

### Attaching Relationships

[](#attaching-relationships)

You can attach relationships with the following code:

```
use App\Models\Tag;
use App\Models\Post;
use App\Models\Video;

$tag = Tag::find(1);

$post = Post::find(1);

$tag->taggables()->attach($post);

$video = Video::find(1);
```

You can also attach a model with pivot attributes:

```
$tag->taggables()->attach($video, ['score' => 1337]);
```

### Detaching Relationships

[](#detaching-relationships)

To detach relationships, use the following code:

```
use App\Models\Tag;
use App\Models\Video;

$tag = Tag::find(1);

$video = Video::find(1);

$tag->taggables()->detach($video);
```

📜 License
---------

[](#-license)

This package is open-source and is released under the MIT License. Please refer to the [LICENSE](LICENSE) file for more information.

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

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

Total

2

Last Release

823d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/28da65e8fb67327a4aecc884be2d4d14d719ed0b393ec9c4febe0f91b9750eb5?d=identicon)[xalaida](/maintainers/xalaida)

---

Top Contributors

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

---

Tags

laraveleloquentrelationmany-to-morphmany-to-any

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/nevadskiy-laravel-many-to-morph/health.svg)

```
[![Health](https://phpackages.com/badges/nevadskiy-laravel-many-to-morph/health.svg)](https://phpackages.com/packages/nevadskiy-laravel-many-to-morph)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[watson/validating

Eloquent model validating trait.

9803.5M54](/packages/watson-validating)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.3M18](/packages/reedware-laravel-relation-joins)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[cybercog/laravel-love

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

1.2k332.0k1](/packages/cybercog-laravel-love)

PHPackages © 2026

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