Laravel 4:使值/列的组合独一无二

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16990723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:29:08  来源:igfitidea点击:

Laravel 4: making a combination of values/columns unique

laravellaravel-4

提问by Dieter

I'm importing a bunch of csv entries in my database with Laravel 4.

我正在使用 Laravel 4 在我的数据库中导入一堆 csv 条目。

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?

我不能真正指出必须是唯一的一列,它是 5 列的组合,使其独一无二。但是:如何在 Laravel 中定义这一点?

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.

选项 1:模式构建器
您可以使用 $table->unique('email') 方法,但这似乎只允许一列,而不是列的组合。

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.

选项 2:验证
不太可取,但我可以在插入模型之前对其进行验证。然而,再次使用'unique:[table]'验证规则,当只有一个列值不是唯一的,而不是它们的组合时,它会返回错误。

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 :-)

谁能告诉我我应该怎么做?
我确定我遗漏了一些东西,但我可以朝正确的方向推动 :-)

Thanks,

谢谢,

Dieter

迪特

回答by Antonio Carlos Ribeiro

You can combine:

您可以组合:

$table->unique( array('email','name') );

And pretty much everything in Laravel will accept arrays to do whatever you need to with 'more than one'.

并且 Laravel 中的几乎所有东西都可以接受数组来做任何你需要的“不止一个”的事情。

回答by Felix

Use Schema Builder's unique()method to define your data model, as Antonio mentioned.

unique()正如 Antonio 提到的那样,使用 Schema Builder 的方法来定义您的数据模型。

Additionally, if you want to use validation on your model, consider my custom Validatorrule for multiple UNIQUEindexes: https://github.com/felixkiss/uniquewith-validator

此外,如果您想在模型上使用验证,请考虑我Validator对多个UNIQUE索引的自定义规则:https: //github.com/felixkiss/uniquewith-validator

回答by Allan Nila Chongwe

You can also do this;

你也可以这样做;

$table->unique(["column1", "column2"], 'uq_columns');

$table->unique(["column1", "column2"], 'uq_columns');

Which means that you will have a unique column combination of all the columns i.e. column1 and column2

这意味着您将拥有所有列的唯一列组合,即 column1 和 column2

回答by JuanDMeGon

I know this question is for Laravel 4, but I just came across this on searches and found a solution for Laravel >= 5.3

我知道这个问题是针对 Laravel 4 的,但我刚刚在搜索中遇到了这个问题,并找到了 Laravel >= 5.3 的解决方案

Here it is:

这里是:

Of course, the migration may look something like

当然,迁移可能看起来像

$table->unique( array('email','name') );

Then to validate this, you do not need to use custom rules, just advanced rules:

然后要验证这一点,您不需要使用自定义规则,只需使用高级规则:

'email' => Rule::unique('users')->where(function ($query) use ($request) {
    return $query->where('name', $request->name);
}),

Of course, you may want to validate name before of this. The name should be required so that you may finish with something like this:

当然,您可能希望在此之前验证名称。该名称应该是必需的,以便您可以完成以下内容:

'name' => 'required|max:255',
'email' => Rule::unique('users')->where(function ($query) use ($request) {
    return $query->where('name', $request->name);
}),

I hope it helps.

我希望它有帮助。

回答by srsajid

You can try this

你可以试试这个

$table->string("name");
$table->string("email")->unique("name")