2014-12-05 37 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值。