New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

staudenmeir/laravel-merged-relations

Merged Laravel Eloquent relationships
431,795 185

Larablocks Analysis

Compatibility

Trusted maintainer

staudenmeir/laravel-merged-relations works with Laravel 13 (latest release v1.11, Mar 1, 2026).

Laravel 5 to 12 in earlier releases.

  • Trusted maintainer: staudenmeir publishes 15 packages on Larablocks, with 5,204,163 downloads a month between them.

Recommendation checks

Recommended

Recommended by Larablocks: meets all 4 checks.

  • Works with Laravel 13, the current version
  • Released in the last 12 months (Mar 1, 2026)
  • 52,615 downloads a month (at least 10,000)
  • 185 GitHub stars (at least 100)

State

Maintained
  • Last stable release: Mar 1, 2026.
  • 1 release in the last 12 months.
  • First released: Jun 23, 2019.
  • Monthly downloads: 52,615, up 311% from six months ago.
  • 1 open issue on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Dec 1, 2026.

Install
composer require staudenmeir/laravel-merged-relations
Latest Version:v1.11
PHP:^8.3
Laravel:13
Older releases:5 – 12
License:MIT
Last Updated:Mar 29, 2026
Links: GitHub  ·  Packagist
Maintainer: staudenmeir

Laravel Merged Relations

CI Code Coverage PHPStan Latest Stable Version Total Downloads License

This Laravel Eloquent extension allows merging multiple relationships using SQL views.
The relationships can target the same or different related models.

Supports Laravel 5.5+.

Installation

composer require staudenmeir/laravel-merged-relations:"^1.0"

Use this command if you are in PowerShell on Windows (e.g. in VS Code):

composer require staudenmeir/laravel-merged-relations:"^^^^1.0"

Versions

Laravel Package
13.x 1.11
12.x 1.10
11.x 1.9
10.x 1.6
9.x 1.5
8.x 1.4
7.x 1.3
6.x 1.2
5.8 1.1
5.5–5.7 1.0

Usage

Use Cases

Use the package to merge multiple polymorphic relationships:

class Tag extends Model
{
    public function allTaggables()
    {
        // TODO
    }

    public function posts()
    {
        return $this->morphedByMany(Post::class, 'taggable');
    }

    public function videos()
    {
        return $this->morphedByMany(Video::class, 'taggable');
    }
}

Or use it to merge relationships with different depths:

class User extends Model
{
    public function allComments()
    {
        // TODO
    }

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

    public function postComments()
    {
        return $this->hasManyThrough(Comment::class, Post::class);
    }
}

Step 1: Creating Views

Before you can define the new relationship, you need to create the merge view in a migration:

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::createMergeView(
    'all_taggables',
    [(new Tag)->posts(), (new Tag)->videos()]
);

By default, the view doesn't remove duplicates. Use createMergeViewWithoutDuplicates() to get unique results:

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::createMergeViewWithoutDuplicates(
    'all_comments',
    [(new User)->comments(), (new User)->postComments()]
);

You can also replace an existing view:

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::createOrReplaceMergeView(
    'all_comments',
    [(new User)->comments(), (new User)->postComments()]
);

The package includes staudenmeir/laravel-migration-views. You can use its methods to rename and drop views:

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::renameView('all_comments', 'user_comments');

Schema::dropView('all_comments');

If you are using php artisan migrate:fresh, you can drop all views with --drop-views.

Step 2: Defining Relationships

With the view created, you can define the merged relationship.

Use the HasMergedRelationships trait in your model and provide the view name:

class Tag extends Model
{
    use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;

    public function allTaggables(): \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation
    {
        return $this->mergedRelation('all_taggables');
    }
}

If all original relationships target the same related model, you can use mergedRelationWithModel(). This allows you to access local scopes and use methods like whereHas() or withCount():

class User extends Model
{
    use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;

    public function allComments(): \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation
    {
        return $this->mergedRelationWithModel(Comment::class, 'all_comments');
    }
}

You can use the merged relationship like any other relationship:

$taggables = Tag::find($id)->allTaggables()->latest()->paginate();

$users = User::with('allComments')->get();

Pivot Table Data

You can retrieve additional pivot columns if your merge view consists of many-to-many relationships.

Add the desired pivot columns to all relationships:

use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema::createMergeView(
    'all_taggables',
    [
        (new Tag)->posts()->withPivot('tagged_at'),
        (new Tag)->videos()->withPivot('tagged_at'),
    ]
);

$taggables = Tag::find($id)->allTaggables;

foreach ($taggables as $taggable) {
    dump($taggable->pivot->tagged_at);
}

Limitations

In the original relationships, it's currently not possible to limit the selected columns or apply withCount().

In the merged relationships, it's not possible to remove global scopes like SoftDeletes. They can only be removed in the original relationships.

Testing

If you use PHPUnit or a similar tool to run tests, add this property to your base test class to ensure that database views are dropped when the test database is cleaned up:

protected bool $dropViews = true;

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

670,032,257 9,703
laravel/framework

The Laravel Framework.

640,572,096 35,338
laravel/tinker

Powerful REPL for the Laravel framework.

546,909,908 7,440
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

465,816,364 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

438,010,529 4,660

Version History

Version Released PHP Laravel License
v1.11 ^8.3 ^13.0 MIT
v1.10 ^8.2 ^12.0 MIT
v1.9.4 ^8.2 ^11.0 MIT
v1.9.3 ^8.2 ^11.0 MIT
v1.9.2 ^8.2 ^11.0 MIT