问题描述
laravel 4 文档提到了独特的字段验证.他们在这里解释了如何将 where 子句包含到唯一验证中.唯一表的单个 WHERE 子句,例如:
The laravel 4 documentation mentions unique field validation. They explain here how to include where clauses to the unique validation. A single WHERE clause for the unique table for example:
$validator = Validator::make(
array(
'name' => 'John Doe'
),
array(
'name' => 'unique:table,field,NULL,id,field1,value1'
)
);
- http://laravel.com/docs/validation#rule-unique
现在我假设这会执行以下操作:
Now i assume this does something like:
"SELECT id FROM table WHERE field = 'John Doe' AND field1 = value1 LIMIT 1"
然后检查此查询是否返回结果,如果没有通过验证器.
Then check's if this query returns a result and if not passes the validator.
所以我想知道是否有办法添加更多 where 子句?如果是这样怎么办?
So i was wondering if there was a way to add more where clauses? And if so how?
推荐答案
我原来的问题结束:
我可以将它们堆叠起来,或者如果没有,我必须如何编写验证?所以我可以添加,field2,value2,field3,value3"等.并将它们堆叠起来像这样吗?
Can i just stack them or how do i have to write my validation if not?So can i just add ",field2,value2,field3,value3" ect. and stack themlike this?
回答
是的,我可以像下面那样堆叠它们.因此,如果我想向我的唯一验证器添加多个 where 子句,我会这样做:
Yes i can just stack them like below. So if i would like to add multiple where clauses to my unique validator i would do it like this:
'name' => 'unique:table,field,NULL,id,field1,value1,field2,value2,field3,value3'
例外
根据 Laravel 文档
unique:table,column,except,idColumn
我将此参数保留为 NULL,因为它与原始问题无关.想知道这个NULL在唯一规则中的作用是什么的,让我详细说明一下:
except 参数强制一个唯一的规则忽略给定的 ID.因此,将其指定为 NULL 将导致检查每条记录(即不忽略任何内容).
'name' => 'unique:users,email,1,id'
这篇关于laravel 4:验证唯一(数据库)多个 where 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!