How to update unique field in Laravel

Asked by: tomlance79
Date:
Viewed: 345
Answers: 1
  • 0

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

Answers

Answer by: ChristianKovats

Answered on: 21 Jul 2023

  • 0

You need to exclude the current user from the model

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

Please log in to post an answer!