我有一个名为JuridicoFormRequest的表单请求,其方法为“rules”:
public function rules() {
$rules = [
'rif'=>'required|max:40|unique:juridico,rif',
'correo'=>'required|max:40|unique:juridico,correo',
'd_social'=>'required|max:50',
'r_social'=>'required|max:50',
'pagina_web'=>'max:40|unique:juridico,pagina_web',
'capital'=>'required|numeric',
'fk_lugar'=>'required|integer',
'fk_lugar_fiscal'=>'required|integer',
'fk_tienda'=>'integer',
'num_carnet'=>'max:50'
];
if ($this->juridico){
$rules['rif'] = 'required|max:40';
$rules['correo'] = 'required|max:40';
$rules['pagina_web'] = 'required|max:40';
}
return $rules;
}
问题是在DataBase中,“Correo”属性必须是唯一的,“pagina_web”也是如此。当我尝试更新某个注册表“A”并且我写另一个注册表“B”的相同“Correo”或另一个注册表的相同“pagina_web”时,会抛出一个错误。 “rif”不会发生这种情况,因为它是主键,我已经用以下方法对其进行了验证:
if ($this->juridico) { /*....*/ }
如果我这样做:
if ($this->juridico) {
$rules['rif'] = 'required|max:40';
$rules['correo'] = 'required|max:40|unique:juridico,correo';
$rules['pagina_web'] = 'required|max:40|unique:juridico,pagina_web';
}
向我抛出验证消息(非例外)“correo必须是唯一的”,而“correo”和“pagina_web”已经被我正在更新的注册表占用。
有没有办法解决这个问题?
更新:我这样解决了:
public function rules(){
$rules = [
'rif'=>'required|max:40|unique:juridico,rif',
'correo'=>'required|max:40|unique:juridico,correo',
'd_social'=>'required|max:50',
'r_social'=>'required|max:50',
'pagina_web'=>'max:40|unique:juridico,pagina_web',
'capital'=>'required|numeric',
'fk_lugar'=>'required|integer',
'fk_lugar_fiscal'=>'required|integer',
'fk_tienda'=>'integer',
'num_carnet'=>'max:50'
];
if ($this->juridico){
$rules['rif'] = 'required|max:40';
$rules['correo'] = 'required|max:40|unique:juridico,correo,'.$this->juridico.',rif';
$rules['pagina_web'] = 'required|max:40|unique:juridico,pagina_web,'.$this->juridico.',rif';
}
return $rules;
}
答案 0 :(得分:3)
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
如果表使用的主键列名称不是id,则可以在调用ignore方法时指定列的名称:
'email' => Rule::unique('users')->ignore($user->id, 'user_id')
您可以自定义它!
答案 1 :(得分:1)
请参阅文档https://laravel.com/docs/5.5/validation#rule-unique
$rules['correo'] = 'required|max:40|unique:juridico,correo,pagina_web';
或
$rules['correo'] = [
'required',
'max:40',
Rule::unique('juridico')->where(function ($query) use ($paginaWeb) {
return $query->where('pagina_web', $paginaWeb);
})
];
在AppServiceProvider中添加此
public function boot()
{
Validator::extend('uniq', function ($attribute, $value, $parameters, $validator) {
$data = $validator->getData();
$tableName = $parameters[0];
unset($parameters[0]);
$primaryKey = 'id';
$query = DB::table($tableName);
// set main uniqueness condition
$query->where($attribute, '=', $value);
// if primary key exists - set to NOT be equal (for updating case)
if (!empty($data[$primaryKey])) {
$query->where($primaryKey, '!=', $data[$primaryKey]);
}
// check conditional columns
if (!empty($parameters)) {
foreach ($parameters as $column) {
if (isset($data[$column])) {
$query->where($column, '=', $data[$column]);
}
}
}
$count = $query->count();
return ($count == 0) ? true : false;
});
}
和用法
$rules['correo'] = 'required|max:40|uniq:juridico,correo,pagina_web';