Laravel 4 检查唯一性 /数据库/ 忽略当前

我在编辑用户的形式上支付检查,并检查用户名是否在数据库中是唯一的。

但是,当您编辑用户并且不更改用户名字段时,它仍然检查此字段是否唯一或不...

有没有办法 laravel 忽略当前用户名是否未更改?
已邀请:

冰洋

赞同来自:

当然,有必要阅读更多文件, laravel 4 可以...

我有


$rules = array/'username' => 'required|unique:users'/;


我有的


$rules = array/'username' => 'required|unique:users,username,'.$id/;


你可以告诉验证器忽略这个 ID, 如上所述。

诸葛浮云

赞同来自:

这个决定为我工作:


protected $rules = array/
'email' => "required|email|unique:users,email,:id",
/;

三叔

赞同来自:

为了 Laravel 4.1, 如果延长课程
Illuminate\Validation\Validator

, 覆盖方法
validateUnique//

因此:


/**
* 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;

// If the specified ID column is present in the data, use those values; otherwise,
// fall back on supplied parameters.
list/$idColumn, $id/ = [null, null];
if /isset/$parameters[2]// {
list/$idColumn, $id/ = $this->getUniqueIds/$parameters/;

if /strtolower/$id/ == 'null'/ $id = null;
else if /strtolower/$id/ == 'id' && isset/$this->data[$idColumn]// $id = $this->data[$idColumn];
}
else if /isset/$this->data['id']// {
$idColumn = 'id';
$id = $this->data[$idColumn];
}

// 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;
}


在您的验证器规则中,您可以执行此操作 /检查领域的存在
id

在数据验证中 - 这是最广泛的用途。/:


$rules = 'unique:model,field';


这是 /检查领域的存在
id

在数据检查中/ /相当于上面的/:


// specifying anything other than 'id' or 'null' as the 3rd parameter
// will still use the current behavior
$rules = 'unique:model,field,id';


或者那个 /检查领域的存在
idColumn

在数据检查中/:


// specifying anything other than 'id' or 'null' as the 3rd parameter
// will still use the current behavior
$rules = 'unique:model,field,id,idColumn';

小明明

赞同来自:

嗨,您可以使用验证程序作为服务或复制逻辑并应用您需要的内容


$valitator = MyCustomValidator::make//->on/'update'/->setCurrent/$id/;


注册:
https://github.com/bradleySuir ... e.php

要回复问题请先登录注册