PHP Classes

Eloquent Composite Primary Key: Define object properties to use as primary keys

Recommend this page to a friend!
  Info   View files Documentation   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 44 All time: 10,783 This week: 455Up
Version License PHP version Categories
eloquent-composite-k 1.0.0MIT/X Consortium ...5PHP 5, Databases, Libraries, Design P...


This package can be used to define object properties to use as primary keys.

It provides a trait that can be used in Laravel model classes to define multiple properties that will be used as primary keys.

The names of the primary key properties should be defined in model classes as an array assigned to a primary class protected class variable.

Innovation Award
PHP Programming Innovation award nominee
March 2021
Number 10
Usually applications that database tables to store information, define one field that has unique values in all records to be the key to access the record data faster.

However, in certain applications there is the need to use more than one table field as unique value key to access the table records faster.

This package provides a trait that can be used with Laravel model objects to define which fields will be used as primary keys composed with values of more than one field.

Manuel Lemos
Picture of Thiago Przyczynski
  Performance   Level  
Name: Thiago Przyczynski <contact>
Classes: 16 packages by
Country: Brazil Brazil
Age: 35
All time rank: 3203237 in Brazil Brazil
Week rank: 264 Up19 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 8x


Eloquent Composite Key

Package to enable composite key support on Eloquent Models.


Install it with composer:

composer require thiagoprz/eloquent-composite-key


Define the primaryKey as an array and use the HasCompositeKey trait on your model.

class User extends Model 
    use HasCompositeKey;
    protected $primaryKey = ['firstKey', 'secondKey'];

The idea of this package is to allow eloquent models to use composite keys despite that Eloquent doesn't support it officially (see

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file HasCompositeKey.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:10,783
This week:455Up