如何在两列上设置唯一约束?
class MyModel extends Migration {
public function up()
{
Schema::create('storage_trackers', function(Blueprint $table) {
$table->increments('id');
$table->string('mytext');
$table->unsignedInteger('user_id');
$table->engine = 'InnoDB';
$table->unique('mytext', 'user_id');
});
}
}
MyMode::create(array('mytext' => 'test', 'user_id' => 1);
// this fails??
MyMode::create(array('mytext' => 'test', 'user_id' => 2);
最佳答案
第二个参数是手动设置唯一索引的名称。使用数组作为第一个参数来创建跨多个列的唯一键。
$table->unique(array('mytext', 'user_id'));
或者(更整洁一点)
$table->unique(['mytext', 'user_id']);
关于laravel-4 - 模式生成器 Laravel 迁移在两列上是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065697/