How to update unique field in Laravel


I have a member profile page where I also have a field to update username. The username field is unique in the database. My problem is that when I update my profile and don’t chanmge the username, it won’t save because it also tries to update the username.

Here is the validation I’m using

'username' => ['string', 'min:3', 'max:100', 'alpha_dash', 'unique:users,username']


Answer ( 1 )


    You need to exclude the current user from the model

    'username' => ['string', 'min:3', 'max:100', 'alpha_dash', 'unique:users,username,'. $user->id]

Leave an answer