Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

compoships

> 后端框架
Open source

Multi-columns relationships for Laravel's Eloquent ORM

1.2K stars0 likes4 views
WebsiteGitHub

About

Multi-columns relationships for Laravel's Eloquent ORM

Compoships

Compoships offers the ability to specify relationships based on two (or more) columns in Laravel's Eloquent 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

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->team_id is null.

php
namespace App;

use Illuminate\Database\Eloquent\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);
    }
}

Related discussions:

  • Relationship on multiple keys
  • Querying relations with extra conditions not working as expected
  • Querying relations with extra conditions in Eager Loading not working
  • BelongsTo relationship with 2 foreign keys
  • Laravel Eloquent: multiple foreign keys for relationship
  • Laravel hasMany association with multiple columns

Installation

The recommended way to install Compoships is through Composer

bash
$ composer require awobaz/compoships

Usage

Using the Awobaz\Compoships\Database\Eloquent\Model 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

If for some reason 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

... 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).

php
namespace App;

use Illuminate\Database\Eloquent\Model;

class A extends Model
{
    use \Awobaz\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:

php
namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

We can also define many-to-many relationships with composite keys through a pivot table:

…

All standard belongsToMany operations work with composite keys: attach(), detach(), sync(), toggle(), withPivot(), withTimestamps(), eager loading, and existence queries (has(), whereHas()).

Composite-key input shapes for attach() and sync()

Two input shapes are supported for attach(), sync(), syncWithoutDetaching(), and toggle() on composite-key relations.

A list of composite tuples (each tuple is an array aligned with the related-pivot-key columns):

php
$team->projects()->attach([
    ['EU', 2],
    ['US', 1],
]);

A map of json_encode($tuple) => $perRowAttributes, equivalent to Laravel's single-key [id => attributes] shape. The key must be the JSON encoding of the composite tuple, produced via json_encode([...]). Per-row attributes override any shared bulk attributes on key conflict, and any per-row attribute keys colliding with the foreign-pivot-key columns are silently dropped to prevent overriding the parent linkage.

php
$team->projects()->attach([
    json_encode(['EU', 2]) => ['role' => 'reviewer'],
    json_encode(['US', 1]) => ['role' => 'lead'],
], ['note' => 'bulk applied to all']);

Passing an associative array key that is not a JSON-encoded tuple of the correct arity throws Awobaz\Compoships\Exceptions\InvalidUsageException.

Factories

Chances are that you may need factories for your Compoships models. If so, you will probably need to use Factory methods to create relationship models. For example, by using the ->has() method. Just use the Awobaz\Compoships\Database\Eloquent\Factories\ComposhipsFactory trait in your factory classes to be able to use relationships correctly.

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.

php
namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use \Awobaz\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:

php
namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

For a many-to-many scenario, imagine users can be assigned to projects, where both the user and the project are identified by a composite key (team_id and department_id):

php
namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function projects()
    {
        return $this->belongsToMany(
            Project::class,
            'project_user',
            ['user_team_id', 'user_department_id'],
            ['project_team_id', 'project_department_id'],
            ['team_id', 'department_id'],
            ['team_id', 'department_id']
        );
    }
}

Supported relationships

Compoships supports the following Laravel Eloquent relationships:

  • hasOne
  • hasMany
  • belongsTo
  • belongsToMany

Also please note that while nullable columns are supported by Compoships, relationships with only null values are not currently possible.

Note on belongsToMany: Custom pivot models (via using()) with composite keys are supported. Your custom pivot class should extend Awobaz\Compoships\Database\Eloquent\Relations\Pivot instead of Laravel's base Pivot class to ensure correct behavior for save, delete, and queue operations.

Composite primary keys

By default, Eloquent builds the WHERE clause for UPDATE, DELETE, and refresh() / fresh() using only the scalar $primaryKey. On tables whose primary key spans multiple columns (such as (id, tenant_id) in multi-tenant or partitioned schemas), $model->save() on a hydrated row emits a query like UPDATE table SET ... WHERE id = ?, missing the discriminator. The same scalar id can exist under another discriminator value, so the operation silently targets the wrong row.

Compoships lets you opt into composite primary key handling on the write path. Declare a $compositeKey property on the model that enumerates every column in the primary key (including the scalar slot named by $primaryKey). Keep $primaryKey as the scalar column name so getKey(), Model::find($id), route model binding, and queue serialization continue to work unchanged.

php
namespace App;

use Awobaz\Compoships\Compoships;
use Illuminate\Database\Eloquent\Model;

class TenantUser extends Model
{
    use Compoships;

    protected $primaryKey = 'id';

    public $incrementing = false;

    protected $keyType = 'string';

    protected $compositeKey = ['id', 'tenant_id'];

    protected $guarded = [];
}

With this declaration, the following operations scope their WHERE clause by every column in $compositeKey:

  • save() and update() on a hydrated model.
  • delete() (including soft deletes via SoftDeletes::runSoftDelete()).
  • refresh() and fresh().

For example:

php
$user = TenantUser::where('id', 'u1')->where('tenant_id', 't1')->first();
$user->name = 'Alice';
$user->save();
// UPDATE tenant_users SET name = ? WHERE id = ? AND tenant_id = ?

The following operations are not affected. They remain identical to stock Eloquent.

  • Model::find($id) looks up by the scalar primary key only.
  • firstOrCreate, updateOrCreate, and similar helpers build their own WHERE clauses from user input.
  • Route model binding by single id continues to use the scalar key.

Queue serialization (via Illuminate\Queue\SerializesModels, used by queueable jobs, events, and notifications) participates in composite handling for single-model properties on the job: getQueueableId() returns a JSON-encoded array of the composite key columns, and newQueryForRestoration() decodes it back into a query that scopes by every key column on the worker side. Round-tripping a single composite-keyed model through the queue reloads the exact composite row that was queued. Old queued payloads predating this feature (with a scalar id) continue to restore via the parent path, so no queue drain is required on upgrade.

Collection round-trip requires the QueueableCompositeCollection wrapper. A raw Illuminate\Database\Eloquent\Collection of composite-keyed models on a job property will restore as an empty collection. The cause is in Laravel's restoreCollection: it re-keys loaded models by scalar getKey() and looks up by the original queued ids (our JSON-encoded composite strings), so the lookup keys never match. The package ships a wrapper class that sidesteps the issue by capturing composite-key tuples at queue time and rebuilding the collection via composite-aware query at restore time:

php
use Awobaz\Compoships\Queue\QueueableCompositeCollection;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Queue\SerializesModels;

class ProcessUsers
{
    use SerializesModels;

    public QueueableCompositeCollection $users;

    public function __construct(Collection $users)
    {
        $this->users = QueueableCompositeCollection::for($users);
    }

    public function handle(): void
    {
        $users = $this->users->restore();
        // ...
    }
}

The wrapper preserves the original collection order, eager-loaded relations, and the model's connection. It rejects mixed-class collections (throws LogicException) and misconfigured $compositeKey declarations (throws InvalidUsageException) at wrap time. The wrapper is opaque to SerializesModels, so PHP's standard serialization captures its state directly. Each call to restore() issues one database query.

If you declare $compositeKey on a model whose array does not contain the value of $primaryKey, the trait throws Awobaz\Compoships\Exceptions\InvalidUsageException on the first save, delete, or refresh. The array must enumerate the whole primary key.

If you mutate a discriminator col

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPcomposite-keyseloquentlaravelmulti-columns

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架