laravel 4:验证唯一(数据库)多个where子句 [英] laravel 4: validation unique (database) multiple where clauses

查看:517
本文介绍了laravel 4:验证唯一(数据库)多个where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    • 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 them like 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'
      



      异常

      唯一规则中给出的第三个参数是except参数,根据 Laravel文档

      The third parameter given in the unique rule is the except parameter, according to the Laravel documentation.

      unique:table,column,except,idColumn
      

      我将此参数留空,因为它与原始问题不相关。对于那些想知道这个NULL的功能在唯一规则内的人来说,请详细说明一下:

      I left this parameter NULL because it is not that relevant to the original question. For those who would like to know what the function of this NULL is within the unique rule, let me elaborate:

      except参数强制一个唯一的规则来忽略给定的ID 。因此,将其指定为NULL将导致检查每个记录(即忽略任何内容)。

      The except parameter forces a unique rule to ignore a given ID. So specifying it as a NULL will result in checking against every record ( i.e. ignoring nothing ).

      例如: 假设我们希望我们的用户电子邮件是唯一的,但我们的管理员用户也可能拥有一个具有相同电子邮件的普通用户帐户。假设我们的管理员用户是我们创建的第一个用户,其ID为1.所以创建新用户时可以做什么:

      For example: Let's say we want our user email to be unique but our admin user might also have a normal user account with the same email. Assuming our admin user is the first user we created it has the id of 1. So what we could do when creating new users is:

      'name' => 'unique:users,email,1,id'
      

      这篇关于laravel 4:验证唯一(数据库)多个where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆