Home > Back-end >  unique validation on update
unique validation on update

Time:08-06

I want to validate Email and phone no. on update. but if the email already belongs to the id then it shouldn't validate, also if the email belongs to another user it should validate. same applies for the phone no. I tried unique on both email and phone_no it's not working

below is my controller code

public function edit($id)
{
 $userlist= Userlist::find($id);
 //dd($data);
 return view('userlist.edit',compact('userlist'));
}
public function update(Request $request, $id)
{
    $request->validate([
        'firstname' => 'required',
        'lastname' => 'required',
        'email_id' => 'required|email',
        'phone_no' => 'required|numeric',
    ]);

    $userlist = Userlist::find($id);
    $userlist->firstname = $request->firstname;
    $userlist->lastname = $request->lastname;
    $userlist->email_id = $request->email_id;
    $userlist->phone_no = $request->phone_no;
    $userlist->update();
    return redirect()->route('userlist.index')->with('success', 'User updated Successfully!');
}

CodePudding user response:

Assuming that your UserList table is called users, you can do this. If it's name other than users just change it below

'email' => 'required|email|unique:users,email,'. $id .''
  • Related