artesaos / forum

Laravel Brasil Forum
26 stars 16 forks source link

Chave composta primária autoincrement Laravel/My sql #19

Open tmsilva opened 7 years ago

tmsilva commented 7 years ago

Existe alguma solução para a utilização de composite primary key usando My sql e Laravel?

dionejpmc commented 6 years ago

Acredito que seja assim

class Example extends Model {

protected $table = 'examples';

protected $primaryKey = ['first_id', 'second_id'];

public $incrementing = false;

....

Na migration:

Schema::create('examples', function (Blueprint $table) { $table->integer('first_id')->unsigned(); $table->integer('second_id')->unsigned(); $table->text('other_column');

$table->primary(['first_id', 'second_id']);

$table->foreign('first_id')->references('id')->on('firsts');
$table->foreign('second_id')->references('id')->on('seconds');

});