PHPackages                             mistery23/eloquent-composite-primary-keys - 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. mistery23/eloquent-composite-primary-keys

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

mistery23/eloquent-composite-primary-keys
=========================================

Offers composite primary keys on Eloquent models

1.0.8(6y ago)0574MITPHP

Since Oct 23Pushed 6y agoCompare

[ Source](https://github.com/mistery23/eloquent-composite-primary-keys)[ Packagist](https://packagist.org/packages/mistery23/eloquent-composite-primary-keys)[ Docs](https://github.com/mistery23/eloquent-composite-primary-keys)[ RSS](/packages/mistery23-eloquent-composite-primary-keys/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (5)Used By (0)

Eloquent Composite Primary Keys
===============================

[](#eloquent-composite-primary-keys)

A single trait to implement in your Eloquent models to support composite primary keys. The Laravel Schema builder supports creating composite primary keys, but Eloquent models don't support it.

This package is largely inspired by [suggested code on Stack Exchange](https://stackoverflow.com/a/36995763/526501) and has also been released in a package with more than this functionality. I needed a separate package for just this purpose.

IMPORTANT: This is now read-only
--------------------------------

[](#important-this-is-now-read-only)

I have decided to make this project read-only and not further work on this. There are a bunch of performance related downsides when doing this in Laravel, which make it no longer fun and rewarding for me to work on.

In case you still want to use this functionality in Laravel, you can have a look at the [LaravelTreats package](https://github.com/mopo922/LaravelTreats/tree/master/src/Model#laraveltreatsmodeltraitshascompositprimarykey) which contains similar functionality.

Install
-------

[](#install)

Install this package through Composer:

```
composer require mistery23/eloquent-composite-primary-keys

```

Make sure you have a database schema that supports composite primary keys, for example via a migration:

```
Schema::create('products', function (Blueprint $table) {
    $table->integer('first_key');
    $table->integer('another_key');
    $table->primary(['first_key', 'another_key']);
    $table->timestamps();
});
```

Use the trait on the Eloquent model you wish to have composite primary keys on:

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use Mistery23\EloquentCompositePrimaryKeys\HasCompositePrimaryKey;

class Product extends Model
{
	use HasCompositePrimaryKey;
```

Next, you set the `$primaryKey` property on your Eloquent model to an array containing the field names that together form your composite primary key:

```
protected $primaryKey = ['first_key', 'another_key'];
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

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 ~258 days

Total

4

Last Release

2351d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/86dc8a5e2e48c6a2223042e929b4d306ce437495a3c4aaf0e2e945355eda7789?d=identicon)[mistery23](/maintainers/mistery23)

---

Top Contributors

[![SviatoslavBereznitskyi](https://avatars.githubusercontent.com/u/37864596?v=4)](https://github.com/SviatoslavBereznitskyi "SviatoslavBereznitskyi (10 commits)")[![coenjacobs](https://avatars.githubusercontent.com/u/245703?v=4)](https://github.com/coenjacobs "coenjacobs (4 commits)")[![noxify](https://avatars.githubusercontent.com/u/521777?v=4)](https://github.com/noxify "noxify (1 commits)")

---

Tags

laravelmodeleloquentrelations

### Embed Badge

![Health badge](/badges/mistery23-eloquent-composite-primary-keys/health.svg)

```
[![Health](https://phpackages.com/badges/mistery23-eloquent-composite-primary-keys/health.svg)](https://phpackages.com/packages/mistery23-eloquent-composite-primary-keys)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)[sebastiaanluca/laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).

40111.7k1](/packages/sebastiaanluca-laravel-boolean-dates)

PHPackages © 2026

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