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

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

ogestor/eloquent-composite-primary-keys
=======================================

Offers composite primary keys on Eloquent models

6.0(5y ago)03MITPHP

Since Oct 23Pushed 5y agoCompare

[ Source](https://github.com/ogestor/eloquent-composite-primary-keys)[ Packagist](https://packagist.org/packages/ogestor/eloquent-composite-primary-keys)[ RSS](/packages/ogestor-eloquent-composite-primary-keys/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (3)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.

Install
-------

[](#install)

Install this package through Composer:

```
composer require ogestor/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 CoenJacobs\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 = array('first_key', 'another_key');
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Total

2

Last Release

1994d ago

Major Versions

1.0.0 → 6.02020-11-26

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c8c9cf1e0ca736e0fc81ccedf7a1cf039821f9aaacfa93a6b9fcab6076e27b2?d=identicon)[ogestor](/maintainers/ogestor)

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

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