How I can put composite keys in models in Laravel 5?
In Laravel 5, you can specify a composite key on a model by defining a protected $primaryKey property on the model and setting it to an array of the column names that make up the primary key. For example:
<?php
class ExampleModel extends Model
{
protected $primaryKey = ['column_name_1', 'column_name_2'];
}
Watch a video course
Learn object oriented PHP
You also have to make sure that the columns you use as the primary key in your migration have unique indexes.
<?php
$table->unique(['column_name_1', 'column_name_2']);
Additionally, you may also need to override some of the default Eloquent methods, such as getKeyName() or getKeyType() to make sure that Eloquent is able to work with composite keys correctly.