如何在两列上设置唯一约束?
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);
Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel 4.2和mysql db.
我有一个考试表,我正在参加考试,字段是 - >id | examdate | batch | chapter | totalmarks
我$table->unique( array('examdate','batch','chapter') );在模式构建器中使用组合的唯一键.
现在我想为它添加一个验证规则.我知道我可以通过laravel唯一验证器规则添加唯一验证,但问题是,它只检查一个字段.
我希望它为3个字段组合添加唯一性(用户必须无法添加具有相同值组合的examdate,batch和chapter字段的第二行).
是否有可能在laravel 4中进行.如果不可能,有任何解决方法吗?