Laravel 4:使值/列的组合唯一 [英] Laravel 4: making a combination of values/columns unique
问题描述
我正在使用Laravel 4在数据库中导入一堆csv条目.
I'm importing a bunch of csv entries in my database with Laravel 4.
我不能真正指出必须具有唯一性的一列,它是5列的组合使其具有唯一性.但是:如何在Laravel中定义这一点?
I can't really point at one column that has to be unique, it's a combination of 5 columns that makes it unique. However: how does one define this in Laravel?
选项1:架构生成器
您可以使用$ table-> unique('email')方法,但这似乎只允许一列,而不是列的组合.
Option 1: schema builder
You can use the $table->unique('email') method, but that only seems to allow one column, not a combination of columns.
选项2:验证
不太理想,但是我可以在插入模型之前对其进行验证.但是,再次使用"unique:[table]"验证规则,当只有一个列值不是唯一的而不是它们的组合时,它将返回错误.
Option 2: Validation
Less preferable, but I could validate the model before inserting it. However, again, using 'unique:[table]' validation rules, it will return an error when just one of the column values isn't unique, not a combination of them.
有人可以告诉我该怎么做吗?
我确定我缺少了一些东西,但是我可以向正确的方向推:-)
Can anyone tell me how I should go about this?
I'm sure I'm missing something, but I could use a push in the right direction :-)
谢谢
Dieter
推荐答案
您可以组合:
$table->unique( array('email','name') );
Laravel中的几乎所有东西都将接受数组以使用多个"来完成您所需的一切.
And pretty much everything in Laravel will accept arrays to do whatever you need to with 'more than one'.
这篇关于Laravel 4:使值/列的组合唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!