I need to update an email with a unique role in the Form Request Validation. I have no problem with creating the user, but the problem is when I try to update the email, it shows that the email is already taken. how can I do something like if the email that is in the input value is owned by the user it can be skipped by the unique role.
namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
public function rules()
{
return [
'email' => ['sometimes', 'required','email:rfc,dns', 'unique:users,email', 'max:255'],
in controller
public function update(UsersRequest $request, $id)
{
$user = User::find($id);
$user->email = $request->input('email');
$user->save();}
blade code
<input name="email" type="email" class="form-control @error('email') is-invalid @enderror" value="{{ $user->email }}" required>
Advertisement
Answer
You are in the update method. so obviously you have a user id that is going to be updated so just fetch the user first,
$user = User::findOrFail($id);
Now the validation will be,
'email' => ['sometimes', 'required','email:rfc,dns', ($this->email === $this->user->email) ? '' : 'unique:users,email', 'max:255'],
Let me know its solves your issue or not.