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

ActiveLibrary

friendsofhyperf/compoships
==========================

Hyperf relationships with support for composite/multiple keys.

v3.1.75(5mo ago)222.5k↑74%MITPHP

Since Aug 2Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/friendsofhyperf/compoships)[ Packagist](https://packagist.org/packages/friendsofhyperf/compoships)[ Fund](https://hdj.me/sponsors/)[ GitHub Sponsors](https://github.com/huangdijia)[ RSS](/packages/friendsofhyperf-compoships/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (56)Used By (0)

Compoships
==========

[](#compoships)

[![Latest Stable Version](https://camo.githubusercontent.com/e7da149a9b432938d8331c45a33dc37f924ae55e6e7b0d8a8cb7bc90a74bfcb6/68747470733a2f2f706f7365722e707567782e6f72672f667269656e64736f666879706572662f636f6d706f73686970732f762f737461626c652e737667)](https://packagist.org/packages/friendsofhyperf/compoships)[![Total Downloads](https://camo.githubusercontent.com/30549c786dcdbaa6f137e64dd47158cd7663a11613452ca52a1eaeef9b1c3edc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f667269656e64736f666879706572662f636f6d706f7368697073)](https://packagist.org/packages/friendsofhyperf/compoships)[![License](https://camo.githubusercontent.com/412bc9d18f50251d6452fb9b034c78f7304bf7f249d067c00eabe87a2ff7c807/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f667269656e64736f666879706572662f636f6d706f7368697073)](https://github.com/friendsofhyperf/compoships)

**Compoships** offers the ability to specify relationships based on two (or more) columns in Hyperf's Model ORM. 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;team\_id** is null.

```
namespace App;

use Hyperf\Database\Model\Model;

class User extends Model
{
    public function tasks()
    {
        //WON'T WORK WITH EAGER LOADING!!!
        return $this->hasMany(Task::class)->where('team_id', $this->team_id);
    }
}
```

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

[](#installation)

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

```
composer require friendsofhyperf/compoships
```

Usage
-----

[](#usage)

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

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

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

### Using the `FriendsOfHyperf\Compoships\Compoships` trait

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

If for some reasons you can't derive your models from `FriendsOfHyperf\Compoships\Database\Eloquent\Model`, you may take advantage of the `FriendsOfHyperf\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 `FriendsOfHyperf\Compoships\Database\Eloquent\Model` or use the `FriendsOfHyperf\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 Hyperf\Database\Model\Model;

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

    public function b()
    {
        return $this->hasMany('B', ['foreignKey1', 'foreignKey2'], ['localKey1', 'localKey2']);
    }
}
```

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

```
namespace App;

use Hyperf\Database\Model\Model;

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

    public function a()
    {
        return $this->belongsTo('A', ['foreignKey1', 'foreignKey2'], ['ownerKey1', 'ownerKey2']);
    }
}
```

### Example

[](#example)

As an example, let's pretend we have a task list with categories, managed by several teams of users where:

- a task belongs to a category
- a task is assigned to a team
- a team has many users
- a user belongs to one team
- a user is responsible for one category of tasks

The user responsible for a particular task is the user *currently* in charge for the category inside the team.

```
namespace App;

use Hyperf\Database\Model\Model;

class User extends Model
{
    use \FriendsOfHyperf\Compoships\Compoships;

    public function tasks()
    {
        return $this->hasMany(Task::class, ['team_id', 'category_id'], ['team_id', 'category_id']);
    }
}
```

Again, same syntax to define the inverse of the relationship:

```
namespace App;

use Hyperf\Database\Model\Model;

class Task extends Model
{
    use \FriendsOfHyperf\Compoships\Compoships;

    public function user()
    {
        return $this->belongsTo(User::class, ['team_id', 'category_id'], ['team_id', 'category_id']);
    }
}
```

Contact
-------

[](#contact)

- [Twitter](https://twitter.com/huangdijia)
- [Gmail](mailto:huangdijia@gmail.com)

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance76

Regular maintenance activity

Popularity29

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 97.6% 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 ~22 days

Total

55

Last Release

161d ago

Major Versions

v1.0.0-beta.2 → v3.0.0-rc.112022-09-16

v2.0.19 → v3.0.0-rc.122022-09-16

v2.0.20 → v3.0.0-rc.162022-09-21

v2.0.24 → v3.0.02022-12-29

2.0.x-dev → v3.0.142023-02-10

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8337659?v=4)[Deeka Wong](/maintainers/huangdijia)[@huangdijia](https://github.com/huangdijia)

---

Top Contributors

[![huangdijia](https://avatars.githubusercontent.com/u/8337659?v=4)](https://github.com/huangdijia "huangdijia (41 commits)")[![xuanyanwow](https://avatars.githubusercontent.com/u/28777109?v=4)](https://github.com/xuanyanwow "xuanyanwow (1 commits)")

---

Tags

hyperfv3.1Hyperf composite keysHyperf relationships

### Embed Badge

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

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

###  Alternatives

[friendsofhyperf/sentry

The sentry component for Hyperf.

1864.6k](/packages/friendsofhyperf-sentry)[hyperf/database

A flexible database library.

202.8M257](/packages/hyperf-database)[hyperf/database-pgsql

A pgsql handler for hyperf/database.

12282.0k13](/packages/hyperf-database-pgsql)[hyperf/validation

hyperf validation

122.1M166](/packages/hyperf-validation)[hyperf/crontab

A crontab component for Hyperf.

131.6M62](/packages/hyperf-crontab)[friendsofhyperf/lock

The lock component for Hyperf.

1447.4k](/packages/friendsofhyperf-lock)

PHPackages © 2026

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