【问题标题】:How to create unique constraints in laravel 5.2?如何在 laravel 5.2 中创建独特的约束?
【发布时间】:2016-04-25 22:29:47
【问题描述】:

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

});

【问题讨论】:

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

标签: php laravel laravel-5.2


【解决方案1】:

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

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

记录在manual中的可用索引类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2017-03-03
    相关资源
    最近更新 更多