Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create unique constraints in laravel 5.2?

How do I create unique constraints using laravel 5.2 Schema Builder?

Schema::create('my_pivot', function(Blueprint $table){

    $table->increments('id');
    $table->integer('table1_id')->unsigned();
    $table->integer('table2_id')->unsigned();

    $table->foreign('table1_id')->references('id')->on('table1');
    $table->foreign('table2_id')->references('id')->on('table2');

    //Here i need to add an unique constraint to 'key1' + 'key2'

});
like image 701
CarlosCarucce Avatar asked Apr 25 '16 14:04

CarlosCarucce


1 Answers

You can pass an array to the unique method.

$table->unique(['key1', 'key2']);

Documented under Available Index Types in the manual

like image 182
Ben Swinburne Avatar answered Nov 20 '22 18:11

Ben Swinburne