PHPackages                             pillar-science/laravel-multi-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. pillar-science/laravel-multi-morph

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

pillar-science/laravel-multi-morph
==================================

Allows to use multiple MorphBy relationships with same model

v0.2(5y ago)174MITPHPPHP ^7.3.0

Since Jan 5Pushed 5y agoCompare

[ Source](https://github.com/pillar-science/laravel-multi-morph)[ Packagist](https://packagist.org/packages/pillar-science/laravel-multi-morph)[ RSS](/packages/pillar-science-laravel-multi-morph/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (1)Versions (3)Used By (0)

Laravel Multi Morph
===================

[](#laravel-multi-morph)

Allows to use multiple MorphOne/MorphMany relationships with same model

### Problem

[](#problem)

When adding multiple morph relationships to the same models, Laravel can't tell the difference between them. In the table related to `MyFile` we will have

When a model have multiple morph relationships with the same model, this cause a problem when fetching information on those relationships. Laravel only store the information on which model type (here `User`) the related model belongs to. As so, when fetching the `profilePicture` or `resume` relations, we get the same result with both files (assuming they exists)

```
class User extends Model
{
    public function profilePicture()
    {
        return $this->morphOne(MyFile::class, 'resource');
    }

    public function resume()
    {
        return $this->morphOne(MyFile::class, 'resource');
    }
}

```

```
class MyFile extends Model
{
    public function resource()
    {
        return $this->morphTo();
    }
}

```

### Solution

[](#solution)

laravel-morph-multiple gives access to two more relationships `morphOneRelationship` and `morphManyRelationship` which will add the relation specific information in an additional column on the model with `morphToRelationship`

How to use
----------

[](#how-to-use)

You need to add a column to the **morphedTo Model** to store the relationship name it was stored for.

### Migration

[](#migration)

Here is an example migration for the **morphedTo Model**.

```
Schema::table('files', function (Blueprint $table) {
    $table->string('resource_relationship')->after('resource_type')->nullable();
});

```

### Replace methods within trait

[](#replace-methods-within-trait)

We need to import the trait `HasMorphByRelationships` and use it on **both** side of the relation (**morphOneMany** and **morphTo**). You need to replace the existing relations with the news one from the trait

OriginalReplace by`morphOne``morphOneRelationship``morphMany``morphManyRelationship``morphTo``morphToRelationship`### Code example

[](#code-example)

```
use PillarScience\LaravelMultiMorph\Eloquent\Concerns\HasMorphByRelationships;

/**
 * refered as MorphedOneMany Model
 */
class User extends Model
{
    use HasMorphByRelationships;

    public function profilePicture()
    {
        return $this->morphOneRelationship(MyFile::class, 'resource');
    }

    public function resume()
    {
        return $this->morphOneRelationship(MyFile::class, 'resource');
    }
}

```

```
use PillarScience\LaravelMultiMorph\Eloquent\Concerns\HasMorphByRelationships;

/**
 * refered as MorphedTo Model
 */
class MyFile extends Model
{
    use HasMorphByRelationships;

    public function resource()
    {
        return $this->morphTo();
    }
}

```

### Associating a model

[](#associating-a-model)

```
// $user is a User model

$file = new MyFile();
$file->resource()->associate($user, 'resume');

```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community6

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

Total

2

Last Release

1927d ago

PHP version history (2 changes)v0.1PHP ^7.2.5

v0.2PHP ^7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/2fcff64512306fc216c9218fa756a743c9e6999dac4617597f410b452b305cdb?d=identicon)[pillarscience](/maintainers/pillarscience)

---

Top Contributors

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

---

Tags

eloquent-relationshipslaravel-packagelaravelrelationsRelationshipsmorphpolymorphic

### Embed Badge

![Health badge](/badges/pillar-science-laravel-multi-morph/health.svg)

```
[![Health](https://phpackages.com/badges/pillar-science-laravel-multi-morph/health.svg)](https://phpackages.com/packages/pillar-science-laravel-multi-morph)
```

###  Alternatives

[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)[moneo/laravel-morphmap

Custom morphMap support for Laravel Framework

521.6k](/packages/moneo-laravel-morphmap)

PHPackages © 2026

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