浏览一下Laravel文档,API文档和源代码,我想知道是否有人知道以下唯一规则中的第4个参数id
是什么?
'email' => 'unique:users,email_address,NULL,id,account_id,1'
我目前对此规则的理解是:
users
- 查看此表email_address
- 检查此列NULL
- 我们可以指定要忽略的主键/ ID值,但我们没有打扰,所以这个参数基本上被忽略了id
- 不确定 account_id
- 其他where子句,这是列名1
- where子句account_id
的值
文档: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}}
干杯
答案 0 :(得分:2)
如果我错了,请纠正我,但第4个参数与第3个参数有关,因为它允许我们在忽略3中指定的ID时指定我们要检查的列。如果它不是id
示例,如果主键不是id
而是user_id
,我们可以这样做:
'email' => 'unique:users,email_address,NULL,user_id,account_id,1'
答案 1 :(得分:2)
答案 2 :(得分:1)
你是对的,第四个参数是id的列名,如果不同于' id'如下所示:
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Validation/Validator.php#L991