Home > Software engineering >  How can I update email in Laravel9?
How can I update email in Laravel9?

Time:11-27

public function update(Request $request, $id)
    {
        $user = User::find($id);
        $request->validate([
            'first_name' => 'required',
            'last_name' => 'required',
            'phone_number' => 'required',
            'email' => 'required|email|unique:users,email'.$id,
            'password' => 'required'
        ]);
        $userData = [
            'first_name' => $request->input('first_name'),
            'last_name' => $request->input('last_name'),
            'email' => $request->input('email'),
            'phone_number'=>$request->input('phone_number'),
        ];

        $user->update($userData);
        $user->assignRole($request->input('roles'));
        return redirect()->route('users.index');
    }

I have problem to update email, this is my error:1062 Duplicate entry '[email protected]' for key 'users_email_unique'

CodePudding user response:

Should be:

` 'email' => 'required|email|unique:users,email,'.$id,`
                               
                                   ------------^ missing comma after users
  • Related