micky micky - 6 months ago 10
PHP Question

Making unique field validation while updating

I am trying to validate for the unique record While editing but it always displays the field must be unique. basically i need to ignore the value of that id. id is the primary key.

$validator=Validator::make($request->all(),[
'name'=>'required',
'telephone'=>'required|unique:telephone',
'email'=>'unique:telephone',
'altemail'=>'unique:telephone',
'image'=>'image',

]);

if($validator->fails()){
return redirect('/telephone/addview')
->withErrors($validator);
}

Answer

use additional arguments for unique rule

$validator=Validator::make($request->all(),[
    'name'=>'required',
    'telephone'=>'required|unique:telephone,telephone,'.$yourMdelInstance->id,
    'email'=>'unique:telephone,email,'.$yourMdelInstance->id,
    'altemail'=>'unique:telephone,altemail,'.$yourMdelInstance->id,
    'image'=>'image',
]);