Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can put composite keys in models in Laravel 5?

I have in my DataBase a table with two primary keys (id and language_id) and I need put it in my models. The default primaryKey in Models (Model.php in Laravel 5) is id, and I want that the primaryKeys will be id and id_language. I tried put it with arrays or a String with ',' but it doesn't work. It says me that the array could not be converted in String.

like image 415
Sergioh Lonet Avatar asked Jul 14 '15 19:07

Sergioh Lonet


People also ask

How do you set up the composite key?

To select more than one field to create a composite key, hold down CTRL and then click the row selector for each field. On the Design tab, in the Tools group, click Primary Key. A key indicator is added to the left of the field or fields that you specify as the primary key.

How do you implement a composite primary key?

We will use the below query to create a Composite Key. Query: CREATE TABLE COMPO ( EMP_ID INT, DEPT_ID INT, EMPNAME VARCHAR(25), GENDER VARCHAR(6), SALARY INT --> //This statement will create a //composite Primary Key from PRIMARY KEY (EMP_ID,DEPT_ID) with the help of Column EMP_ID and DEPT_ID );

Can key attributes be composite?

Sometimes more than one attributes are needed to uniquely identify an entity. A primary key that is made by the combination of more than one attribute is known as a composite key. Composite key is a key which is the combination of more than one field or column of a given table. It may be a candidate key or primary key.


2 Answers

I wrote this simple PHP trait to adapt Eloquent to handle composite keys:

<?php  namespace App\Model\Traits; // *** Adjust this to match your model namespace! ***  use Illuminate\Database\Eloquent\Builder;  trait HasCompositePrimaryKey {     /**      * Get the value indicating whether the IDs are incrementing.      *      * @return bool      */     public function getIncrementing()     {         return false;     }      /**      * Set the keys for a save update query.      *      * @param  \Illuminate\Database\Eloquent\Builder $query      * @return \Illuminate\Database\Eloquent\Builder      */     protected function setKeysForSaveQuery(Builder $query)     {         foreach ($this->getKeyName() as $key) {             // UPDATE: Added isset() per devflow's comment.             if (isset($this->$key))                 $query->where($key, '=', $this->$key);             else                 throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key);         }          return $query;     }      // UPDATE: From jessedp. See his edit, below.     /**      * Execute a query for a single record by ID.      *      * @param  array  $ids Array of keys, like [column => value].      * @param  array  $columns      * @return mixed|static      */     public static function find($ids, $columns = ['*'])     {         $me = new self;         $query = $me->newQuery();         foreach ($me->getKeyName() as $key) {             $query->where($key, '=', $ids[$key]);         }         return $query->first($columns);     } } 

Place that in a Traits directory under your main model directory, then you can add a simple one-liner to the top of any composite-key model:

class MyModel extends Eloquent {     use Traits\HasCompositePrimaryKey; // *** THIS!!! ***      /**      * The primary key of the table.      *       * @var string      */     protected $primaryKey = array('key1', 'key2');      ... 


addded by jessedp:
This worked wonderfully for me until I wanted to use Model::find ... so the following is some code (that could probably be better) that can be added to the hasCompositePrimaryKey trait above:
protected static function find($id, $columns = ['*']) {     $me = new self;     $query = $me->newQuery();     $i=0;      foreach ($me->getKeyName() as $key) {         $query->where($key, '=', $id[$i]);         $i++;     }      return $query->first($columns); } 

Update 2016-11-17

I'm now maintaining this as part of an open-source package called LaravelTreats.

Update 2020-06-10

LaravelTreats is dead, but enjoy the code anyways :)

Over the years, a few in-depth use cases have been brought to my attention where this breaks down. This should work for the vast majority of use cases, but know that if you try to get fancy, you might have to rethink your approach.

like image 161
mopo922 Avatar answered Nov 01 '22 14:11

mopo922


You can't. Eloquent doesn't support composite primary keys.

Here's a Github issue regarding this.

like image 42
lukasgeiter Avatar answered Nov 01 '22 14:11

lukasgeiter