PHPackages                             artoodetoo/compoships - 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. artoodetoo/compoships

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

artoodetoo/compoships
=====================

Laravel relationships with support for composite/multiple keys

1.0.4.1(7y ago)0161MITPHPPHP &gt;=5.6.4

Since Apr 4Pushed 7y ago1 watchersCompare

[ Source](https://github.com/artoodetoo/compoships)[ Packagist](https://packagist.org/packages/artoodetoo/compoships)[ RSS](/packages/artoodetoo-compoships/feed)WikiDiscussions master Synced today

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

Compoships
==========

[](#compoships)

**Compoships** offers the ability to specify relationships based on two (or more) columns in Laravel 5's Eloquent. The need to match multiple columns in the definition of an Eloquent relationship often arises when working with third party or pre existing schema/database.

The problem
-----------

[](#the-problem)

Eloquent doesn't support composite keys. As a consequence, there is no way to define a relationship from one model to another by matching more than one column. Trying to use `where clauses` (like in the example below) won't work when eager loading the relationship because at the time the relationship is processed **$this-&gt;f2** is null.

```
namespace App;

use Illuminate\Database\Eloquent\Model;

class Foo extends Model
{
    public function bars()
    {
        //WON'T WORK WITH EAGER LOADING!!!
        return $this->hasMany('Bar', 'f1', 'f1')->where('f2', $this->f2);
    }
}
```

#### Related discussions:

[](#related-discussions)

- [Relationship on multiple keys](https://laracasts.com/discuss/channels/eloquent/relationship-on-multiple-keys)
- [Querying relations with extra conditions not working as expected](https://github.com/laravel/framework/issues/1272)
- [Querying relations with extra conditions in Eager Loading not working](https://github.com/laravel/framework/issues/19488)
- [BelongsTo relationship with 2 foreign keys](https://laravel.io/forum/08-02-2014-belongsto-relationship-with-2-foreign-keys)
- [Laravel Eloquent: multiple foreign keys for relationship](https://stackoverflow.com/questions/48077890/laravel-eloquent-multiple-foreign-keys-for-relationship/49834070#49834070)
- [Laravel hasMany association with multiple columns](https://stackoverflow.com/questions/32471084/laravel-hasmany-association-with-multiple-columns)

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

[](#installation)

The recommended way to install **Compoships** is through [Composer](http://getcomposer.org/)

```
$ composer require awobaz/compoships
```

Usage
-----

[](#usage)

### Using the `Awobaz\Compoships\Database\Eloquent\Model` class

[](#using-the-awobazcomposhipsdatabaseeloquentmodel-class)

Simply make your model class derive from the `Awobaz\Compoships\Database\Eloquent\Model` base class. The `Awobaz\Compoships\Database\Eloquent\Model` extends the `Eloquent` base class without changing its core functionality.

### Using the `Awobaz\Compoships\Compoships` trait

[](#using-the-awobazcomposhipscompoships-trait)

If for some reasons you can't derive your models from `Awobaz\Compoships\Database\Eloquent\Model`, you may take advantage of the `Awobaz\Compoships\Compoships` trait. Simply use the trait in your models.

**Note:** To define a multi-columns relationship from a model *A* to another model *B*, **both models must either extend `Awobaz\Compoships\Database\Eloquent\Model` or use the `Awobaz\Compoships\Compoships` trait**

### Syntax

[](#syntax)

... and now we can define a relationship from a model *A* to another model *B* by matching two or more columns (by passing an array of columns instead of a string).

```
namespace App;

use Illuminate\Database\Eloquent\Model;

class A extends Model
{
    use \Awobaz\Compoships\Compoships;

    public function b()
    {
        return $this->hasMany('B', ['f1', 'f2'], ['f1', 'f2']);
    }
}
```

We can use the same syntax to define the inverse of the relationship:

```
namespace App;

use Illuminate\Database\Eloquent\Model;

class B extends Model
{
    use \Awobaz\Compoships\Compoships;

    public function a()
    {
        return $this->belongsTo('A', ['f1', 'f2'], ['f1', 'f2']);
    }
}
```

Supported relationships
-----------------------

[](#supported-relationships)

**Compoships** only supports the following Laravel 5's Eloquent relationships:

- hasOne
- HasMany
- belongsTo

Disclaimer
----------

[](#disclaimer)

**Compoships** doesn't bring support for composite keys in Laravel 5's Eloquent. This package only offers the ability to specify relationships based on more than one column. We believe that all models' tables should have a single primary key. But there are situations where you'll need to match many columns in the definition of a relationship even when your models' tables have a single primary key.

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

[](#contributing)

Please read [CONTRIBUTING.md](https://github.com/topclaudy/compoships/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests.

Versioning
----------

[](#versioning)

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/topclaudy/compoships/tags).

Unit Tests
----------

[](#unit-tests)

In order to run the test suite, install the development dependencies:

```
$ composer install --dev
```

Then, run the following command:

```
$ vendor/bin/phpunit
```

Authors
-------

[](#authors)

- [Claudin J. Daniel](https://github.com/topclaudy) - *Initial work*

License
-------

[](#license)

**Compoships** is licensed under the [MIT License](http://opensource.org/licenses/MIT).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 60% 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 ~14 days

Total

6

Last Release

2888d ago

PHP version history (2 changes)1.0.0PHP &gt;=7

1.0.4.1PHP &gt;=5.6.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/b982e008c16f8ed2e586e900267b93b01114c28edad839b80b2bad2f4cf1a15d?d=identicon)[artoodetoo](/maintainers/artoodetoo)

---

Top Contributors

[![topclaudy](https://avatars.githubusercontent.com/u/1437237?v=4)](https://github.com/topclaudy "topclaudy (3 commits)")[![artoodetoo](https://avatars.githubusercontent.com/u/577710?v=4)](https://github.com/artoodetoo "artoodetoo (2 commits)")

---

Tags

laravellaravel composite keyslaravel relationships

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/artoodetoo-compoships/health.svg)

```
[![Health](https://phpackages.com/badges/artoodetoo-compoships/health.svg)](https://phpackages.com/packages/artoodetoo-compoships)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[awobaz/compoships

Laravel relationships with support for composite/multiple keys

1.2k10.6M40](/packages/awobaz-compoships)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[clickbar/laravel-magellan

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

423715.4k1](/packages/clickbar-laravel-magellan)[baril/bonsai

An implementation of the Closure Tables pattern for Eloquent.

3593.5k](/packages/baril-bonsai)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)

PHPackages © 2026

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