laravel 7 profile udpate wiith email unique:users

I am trying to update the user profile. Everything works good but when i try to update name without updating the email. It gives an error saying the email already exists.
I have tried the method in the docs too but it does not work. I am using laravel 7
Here is my code

        $userId = auth()->user()->id;
    $validate = $request->validate([
        'name' => 'required|string',
        'email' => ['required', Rule::unique('users')->ignore($userId)],
        'password' => 'required|',

    $user = User::find(auth()->user()->id);
    $password = $request->password;

        'name' => $validate['name'],
        'email' => $validate['email'],
        'password' => Hash::make($password),

Note: I just fixed it

I was not updating the profile i was creating a new one. I was using $user::create but this code creates another row in table the which is why the validation was not working so i replaced it with this code

       $user->name = $request->name;
    $user->email = $request->email;
    $user->password = Hash::make($password);

Now it works.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you can use update in place of create like this:

    'name' => $validate['name'],
    'email' => $validate['email'],
    'password' => Hash::make($password),

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x