How to update unique field in Laravel

Question

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']

Thanks

Answer ( 1 )

    0
    2023-01-25T18:29:33+00:00

    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