PHPackages                             korridor/laravel-has-many-merged - 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. korridor/laravel-has-many-merged

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

korridor/laravel-has-many-merged
================================

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships

1.3.0(2mo ago)91137.1k↓56.9%3[4 issues](https://github.com/korridor/laravel-has-many-merged/issues)[2 PRs](https://github.com/korridor/laravel-has-many-merged/pulls)1MITPHPPHP &gt;=8.1CI passing

Since Feb 15Pushed 3w ago3 watchersCompare

[ Source](https://github.com/korridor/laravel-has-many-merged)[ Packagist](https://packagist.org/packages/korridor/laravel-has-many-merged)[ Docs](https://github.com/korridor/laravel-has-many-merged)[ RSS](/packages/korridor-laravel-has-many-merged/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (8)Dependencies (12)Versions (14)Used By (1)

Laravel has many merged
=======================

[](#laravel-has-many-merged)

[![Latest Version on Packagist](https://camo.githubusercontent.com/aeb98a5786c6a224af3c545f31452060314810eef7e73c1044953a4f29e7ecd0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b6f727269646f722f6c61726176656c2d6861732d6d616e792d6d65726765643f7374796c653d666c61742d737175617265)](https://packagist.org/packages/korridor/laravel-has-many-merged)[![License](https://camo.githubusercontent.com/6410f8798dcde56926040e51195911d8af9c54d86a19d525c18f5396d6d4cec3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b6f727269646f722f6c61726176656c2d6861732d6d616e792d6d65726765643f7374796c653d666c61742d737175617265)](license.md)[![GitHub Workflow Lint](https://camo.githubusercontent.com/527b2d9eeb453669f263bba3c63d66d0c2f3eefd57b7ab5e242a3edb478dd879/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b6f727269646f722f6c61726176656c2d6861732d6d616e792d6d65726765642f6c696e742e796d6c3f6c6162656c3d6c696e74267374796c653d666c61742d737175617265)](https://github.com/korridor/laravel-has-many-merged/actions/workflows/lint.yml)[![GitHub Workflow Tests](https://camo.githubusercontent.com/eb452761a015d02b7f5c912ada20776f5c3421ce450ab00caeb99b8605eddf12/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b6f727269646f722f6c61726176656c2d6861732d6d616e792d6d65726765642f756e697474657374732e796d6c3f6c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/korridor/laravel-has-many-merged/actions/workflows/unittests.yml)[![Codecov](https://camo.githubusercontent.com/23f1a84495c21738205c36b95da44eec051a3b96586e5cf7417a8939e6b893ed/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6b6f727269646f722f6c61726176656c2d6861732d6d616e792d6d65726765643f7374796c653d666c61742d737175617265)](https://codecov.io/gh/korridor/laravel-has-many-merged)

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships. This relation fully supports lazy and eager loading.

Note

Check out **solidtime - The modern Open Source Time-Tracker** at [solidtime.io](https://www.solidtime.io)

Installation
------------

[](#installation)

You can install the package via composer with following command:

```
composer require korridor/laravel-has-many-merged
```

If you want to use this package with older Laravel/PHP version please install the 0.\* version.

```
composer require korridor/laravel-has-many-merged "^0"
```

### Requirements

[](#requirements)

This package is tested for the following Laravel versions:

- 13.\* (PHP 8.3, 8.4)
- 12.\* (PHP 8.2, 8.3, 8.4)
- 11.\* (PHP 8.2, 8.3, 8.4)
- 10.\* (PHP 8.1, 8.2, 8.3)

Usage examples
--------------

[](#usage-examples)

In the following example there are two models User and Message. Each message has a sender and a receiver. The User model has two hasMany relations - one for the sent messages and the other for the received ones.

With this plugin you can add a relation that contains sent and received messages of a user.

```
use Korridor\LaravelHasManyMerged\HasManyMerged;
use Korridor\LaravelHasManyMerged\HasManyMergedRelation;

class User extends Model
{
    use HasManyMergedRelation;

    // ...

    /**
     * @return HasManyMerged
     */
    public function messages(): HasManyMerged
    {
        return $this->hasManyMerged(Message::class, ['sender_user_id', 'receiver_user_id']);
    }

    /**
     * @return HasMany
     */
    public function sentMessages(): HasMany
    {
        return $this->hasMany(Message::class, 'sender_user_id');
    }

    /**
     * @return HasMany
     */
    public function receivedMessages(): HasMany
    {
        return $this->hasMany(Message::class, 'receiver_user_id');
    }
}
```

Contributing
------------

[](#contributing)

I am open for suggestions and contributions. Just create an issue or a pull request.

### Local docker environment

[](#local-docker-environment)

The `docker` folder contains a local docker environment for development. The docker workspace has composer and xdebug installed.

```
docker-compose run workspace bash
```

### Testing

[](#testing)

The `composer test` command runs all tests with [phpunit](https://phpunit.de/). The `composer test-coverage` command runs all tests with phpunit and creates a coverage report into the `coverage` folder.

### Codeformatting/Linting

[](#codeformattinglinting)

The `composer fix` command formats the code with [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). The `composer lint` command checks the code with [phpcs](https://github.com/squizlabs/PHP_CodeSniffer).

License
-------

[](#license)

This package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.

###  Health Score

59

—

FairBetter than 98% of packages

Maintenance89

Actively maintained with recent releases

Popularity46

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% 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 ~269 days

Recently: every ~288 days

Total

8

Last Release

79d ago

Major Versions

0.0.3 → 1.0.02023-02-16

PHP version history (2 changes)0.0.1PHP ^7.1|^8

1.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/26689068?v=4)[Constantin Graf](/maintainers/korridor)[@korridor](https://github.com/korridor)

---

Top Contributors

[![korridor](https://avatars.githubusercontent.com/u/26689068?v=4)](https://github.com/korridor "korridor (10 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")

---

Tags

eloquentlaravellaravel-eloquentlaravel-packagephplaraveleloquentrelationshas-many

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/korridor-laravel-has-many-merged/health.svg)

```
[![Health](https://phpackages.com/badges/korridor-laravel-has-many-merged/health.svg)](https://phpackages.com/packages/korridor-laravel-has-many-merged)
```

###  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)[cybercog/laravel-love

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

1.2k332.0k1](/packages/cybercog-laravel-love)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.3M18](/packages/reedware-laravel-relation-joins)[korridor/laravel-has-many-sync

Laravel has many sync

3698.7k](/packages/korridor-laravel-has-many-sync)

PHPackages © 2026

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