0

Is there any way to get original data in request? I mean, in my case I want to validate updating user's in the form. I want the email to be unique on emails in database except on the email that user wrote to the form.

1

3 Answers 3

1

Laravel already has a validation rule for this, you can add an id of the user to ignore to your unique validator.

'email' => [
    'required',
    Rule::unique('users')->ignore($currentUser->id),
]
Sign up to request clarification or add additional context in comments.

Comments

0

I used something like this:

'email' => ['sometimes', 'email', 'unique:users,email,'.$user->getKey().','.$user->getKeyName().',deleted_at,NULL', 'string'],

Not sure if it is the best solution but works. I kind of do not like it because the $user is based on hidden input from the request.

Comments

0

You can do it liks this:

'email' => 'unique:users,email,'.$user->id

The user id will allow you to keep updating the record but maintain the unique constraint on email. You don't need a hidden input field for $user->id btw.

1 Comment

I do because form I am updating is binded like so: {!! Form::model($customerAddress, ['route' => 'user.update', 'class' => 'form form-horizontal form-user-data']) !!} and I cannot access $this->user in the request.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.