问题描述
我正在使用 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 :-)
谢谢,
节食
推荐答案
可以组合:
$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:使值/列的组合独一无二的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!