2014-12-05 73 views
0

看看Laravel文档,API文档和源代码我想知道是否有人知道下面的唯一规则中的第四个参数id是针对什么的?Laravel验证:独特的规则第四参数

'email' => 'unique:users,email_address,NULL,id,account_id,1' 

我目前的这条规则的理解是:

  • users - 寻找此表
  • email_address - 检查对本栏目
  • NULL - 将在那里我们可以指定一个主键/ ID值忽略,但我们没有打扰,所以这个参数基本上被忽略
  • id - 不确定
  • account_id - 额外的where子句中,这是列名
  • 1 - 在account_id值where子句

文档:http://laravel.com/docs/4.2/validation

功能负责执行的唯一规则验证发现在\Illuminate\Validation\Validator功能validateUnique($attribute, $value, $parameters)在线949

/** 
* Validate the uniqueness of an attribute value on a given database table. 
* 
* If a database column is not specified, the attribute will be used. 
* 
* @param string $attribute 
* @param mixed $value 
* @param array $parameters 
* @return bool 
*/ 
protected function validateUnique($attribute, $value, $parameters) 
{ 
    $this->requireParameterCount(1, $parameters, 'unique'); 

    $table = $parameters[0]; 

    // The second parameter position holds the name of the column that needs to 
    // be verified as unique. If this parameter isn't specified we will just 
    // assume that this column to be verified shares the attribute's name. 
    $column = isset($parameters[1]) ? $parameters[1] : $attribute; 

    list($idColumn, $id) = array(null, null); 

    if (isset($parameters[2])) 
    { 
     list($idColumn, $id) = $this->getUniqueIds($parameters); 

     if (strtolower($id) == 'null') $id = null; 
    } 

    // The presence verifier is responsible for counting rows within this store 
    // mechanism which might be a relational database or any other permanent 
    // data store like Redis, etc. We will use it to determine uniqueness. 
    $verifier = $this->getPresenceVerifier(); 

    $extra = $this->getUniqueExtra($parameters); 

    return $verifier->getCount(

     $table, $column, $value, $id, $idColumn, $extra 

    ) == 0; 
} 

干杯

回答

1

啊坚果,我觉得一分钱刚刚下降。

如果我错了,更正我,但第四个参数与第三个参数有关,因为它允许我们指定忽略3中指定的ID时要检查的列。如果它不是id

例如,如果主键不id,是user_id相反,我们可以这样做:如图所示

'email' => 'unique:users,email_address,NULL,user_id,account_id,1' 
1
  • $ ARG1 - 寻找此表
  • $ ARG2 - 检查此列。默认为验证密钥(eq:email)
  • $ ARG3 - Aditional WHERE不值。
  • $ ARG4 - Aditional WHERE NOT field。默认为主键
  • $ ARG5 - Aditional WHERE字段1 - Aditional WHERE值。