PHP Classes

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

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 45 All time: 10,796 This week: 36Up
Version License PHP version Categories
eloquent-composite-k 1.0.0MIT/X Consortium ...5PHP 5, Databases, Libraries, Design P...
Description 

Author

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: 3194238 in Brazil Brazil
Week rank: 195 Up15 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 8x

Documentation

Eloquent Composite Key

Package to enable composite key support on Eloquent Models.

Installation

Install it with composer:

composer require thiagoprz/eloquent-composite-key

Usage

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 https://laravel.com/docs/8.x/eloquent#composite-primary-keys).


  Files folder image Files (3)  
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 README.md Doc. Documentation

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:45
This week:0
All time:10,796
This week:36Up