如何在laravel 5.2中创建独特的约束?

时间:2016-04-25 14:22:38

标签: php laravel laravel-5.2

如何使用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'

});

1 个答案:

答案 0 :(得分:4)

您可以将数组传递给unique方法。

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

manual

中的可用索引类型下记录