PHPackages                             biladina/laravel-composite-relations - 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. biladina/laravel-composite-relations

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

biladina/laravel-composite-relations
====================================

Adds the ability define composite eloquent relations.

014PHP

Since Apr 17Pushed 11mo agoCompare

[ Source](https://github.com/biladina/laravel-composite-relations)[ Packagist](https://packagist.org/packages/biladina/laravel-composite-relations)[ RSS](/packages/biladina-laravel-composite-relations/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Laravel Composite Relations
===========================

[](#laravel-composite-relations)

[![Laravel Version](https://camo.githubusercontent.com/81b3bebce9e9f17808c2422b0355881ed3125b01c0881861dd94ca2f265209be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31302e7825324631312e782d626c7565)](https://laravel.com/)[![Tests](https://github.com/tylernathanreed/laravel-composite-relations/actions/workflows/tests.yml/badge.svg)](https://github.com/tylernathanreed/laravel-composite-relations/actions/workflows/tests.yml)[![Lint](https://github.com/tylernathanreed/laravel-composite-relations/actions/workflows/coding-standards.yml/badge.svg)](https://github.com/tylernathanreed/laravel-composite-relations/actions/workflows/coding-standards.yml)[![Static Analysis](https://github.com/tylernathanreed/laravel-composite-relations/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/tylernathanreed/laravel-composite-relations/actions/workflows/static-analysis.yml)[![Total Downloads](https://camo.githubusercontent.com/f740b1faba94e495fc99ba7f19760ab6530a1883903c5318e2c34e98e8173ffc/68747470733a2f2f706f7365722e707567782e6f72672f72656564776172652f6c61726176656c2d636f6d706f736974652d72656c6174696f6e732f646f776e6c6f616473)](//packagist.org/packages/reedware/laravel-composite-relations)

This package adds the ability to have multiple foreign keys in a relation.

Introduction
------------

[](#introduction)

Eloquent does not natively support using composite keys in relationships. While single key relationships are typically preferred, there are times where they are unfortunately needed, and there's good way around it. This package offers a solution where you can use composite keys, and everything still feels like it's Eloquent.

This package will allow you to define the following composite relations:

- Belongs To
- Has One
- Has Many

All composite relations support eager loading and existence queries (e.g. "where has").

There currently is no intention to add support for additional relations, as these should be enough for the vast majority of use cases.

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

[](#installation)

#### Using Composer

[](#using-composer)

```
composer require reedware/laravel-composite-relations

```

#### Versioning

[](#versioning)

This package is maintained with the latest version of Laravel in mind, but support follows Laravel's [Support Policy](https://laravel.com/docs/master/releases#support-policy).

PackageLaravelPHP4.x10.x - 11.x8.1 - 8.3+3.x8.x - 10.x7.2 - 8.0+2.x6.x - 8.x7.2 - 8.0+1.x5.5 - 5.87.1 - 7.3+#### Code Changes

[](#code-changes)

This package does not use a service provider or facade, but rather a trait. On your base model instance, you'll want to include the following:

```
use Illuminate\Database\Eloquent\Model as Eloquent;
use Reedware\LaravelCompositeRelations\HasCompositeRelations;

abstract class Model extends Eloquent
{
    use HasCompositeRelations;
}
```

Usage
-----

[](#usage)

### 1. Defining Relations

[](#1-defining-relations)

#### Composite Belongs To

[](#composite-belongs-to)

Imagine you were defining a *non-composite* belongs to relation:

```
public function myRelation()
{
    return $this->belongsTo(MyRelated::class, 'my_column_on_this_table', 'my_column_on_the_other_table');
}
```

Since composite relations use multiple keys, you'll simply define the keys as an array:

```
public function myCompositeRelation()
{
    return $this->compositeBelongsTo(MyRelated::class, ['my_first_key', 'my_second_key'], ['their_first_key', 'their_second_key']);
}
```

#### Composite Has One

[](#composite-has-one)

This follows the same structure as the composite belongs to relation:

```
public function myCompositeRelation()
{
    return $this->compositeHasOne(MyRelated::class, ['their_first_key', 'their_second_key'], ['my_first_key', 'my_second_key']);
}
```

#### Composite Has Many

[](#composite-has-many)

The pattern continues:

```
public function myCompositeRelation()
{
    return $this->compositeHasMany(MyRelated::class, ['foreign_1', 'foreign_2'], ['local_1', 'local_2']);
}
```

### 2. Omitting Foreign and Local Keys

[](#2-omitting-foreign-and-local-keys)

With non-composite relationships, you aren't actually required to provide the foreign and local key, assuming you follow a certain convention. This functionality is also available for composite relations, but must be defined differently. Here's how:

```
class Task extends Models
{
    /**
     * The primary keys for the model.
     *
     * @var array
     */
    protected $primaryKeys = ['vendor_id', 'vendor_name'];

    public function importSummary()
    {
        return $this->compositeHasOne(TaskImportSummary::class);
    }
}

class TaskImportSummary extends Models
{
    public function task()
    {
        return $this->compositeBelongsTo(TaskImportSummary::class);
    }
}
```

### 3. Joining through Composite Relations

[](#3-joining-through-composite-relations)

This package is compatible with [reedware/laravel-relation-joins](https://github.com/tylernathanreed/laravel-relation-joins), meaning you can join through composite relations just like anything else:

```
$task->joinRelation('importSummary', function($join) {
    $join->where('task_import_summaries.name', 'like', '%Relation joins are cool!%');
});
```

You must separately include [reedware/laravel-relation-joins](https://github.com/tylernathanreed/laravel-relation-joins) for this to work.

### 4. Using composite `and` glue

[](#4-using-composite-and-glue)

The default glue between composite keys is `'or'`. Meaning your query will be like:

```
where (("foreign_1" = ? or "foreign_2" = ?) or ("foreign_1" = ? or "foreign_2" = ?))
```

You can change that by passing `'and'` for the glue parameter:

```
public function myCompositeBelongsToRelation()
{
    return $this->compositeBelongsTo(MyRelated::class, ['local_1', 'local_2'], ['foreign_1', 'foreign_2'], null, 'and');
}

public function myCompositeHasOneRelation()
{
    return $this->compositeHasOne(MyRelated::class, ['foreign_1', 'foreign_2'], ['local_1', 'local_2'], 'and');
}

public function myCompositeHasManyRelation()
{
    return $this->compositeHasMany(MyRelated::class, ['foreign_1', 'foreign_2'], ['local_1', 'local_2'], 'and');
}
```

Giving this result :

```
where (("foreign_1" = ? and "foreign_2" = ?) or ("foreign_1" = ? and "foreign_2" = ?))
```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 93.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.

### Community

Maintainers

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

---

Top Contributors

[![tylernathanreed](https://avatars.githubusercontent.com/u/6486381?v=4)](https://github.com/tylernathanreed "tylernathanreed (59 commits)")[![biladina](https://avatars.githubusercontent.com/u/10651085?v=4)](https://github.com/biladina "biladina (2 commits)")[![Rizky92](https://avatars.githubusercontent.com/u/18244409?v=4)](https://github.com/Rizky92 "Rizky92 (1 commits)")[![SwiTool](https://avatars.githubusercontent.com/u/8834915?v=4)](https://github.com/SwiTool "SwiTool (1 commits)")

### Embed Badge

![Health badge](/badges/biladina-laravel-composite-relations/health.svg)

```
[![Health](https://phpackages.com/badges/biladina-laravel-composite-relations/health.svg)](https://phpackages.com/packages/biladina-laravel-composite-relations)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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