How Validate Unique Email Out of the User That Is Updating It in Laravel

Laravel: Validation unique on update

One simple solution.

In your Model

protected $rules = [
'email_address' => 'sometimes|required|email|unique:users',
..
];

In your Controller, action:update

...
$rules = User::$rules;
$rules['email_address'] = $rules['email_address'] . ',id,' . $id;
$validationCertificate = Validator::make($input, $rules);

How validate unique email out of the user that is updating it in Laravel?

You can tell that to validators:

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

Check the docs, in section 'Forcing A Unique Rule To Ignore A Given ID'.

Laravel validation throws error on updating same unique value

You have to use following code

$this->validate($request, [
'name' => ['required', Rule::unique('myTableName')->ignore($yourVariable->id)],
]);

Add header section below code

use Illuminate\Validation\Rule;

More Details here

How can i Skip email Form unique Validation when update user data In Laravel

I'm Fixing It I'm Passing The Id To The Request Like This

<input name='id' value="$item -> id" hidden >

and i write in Validation

  'email' => 'required|email|unique:admins,email,' . $this -> id ,

Validate unique email on update, ignoring self email. On Laravel 5

Passing $u->email isn't quite right for the unique rule. The signature is

unique:{table},{column},{value_to_ignore},{column_to_ignore}

So, with your rule set as

unique:users,email,'.$u->email

It's trying to find a user with an id of $u->email, due to the default column_to_ignore being set to id (or whatever the primary key of your users table is)

To fix this, simply change your unique rule as below:

$u = User::find($id);
$rules = [
"email" => "required|email|max:128|unique:users,email,".$u->id.",id",
...
];

Note: You can omit the ",id" from the rule if you like. As said, this will default to the primary key of your users table. If you changed it from id, then you will need to specify it.

With the rule adjusted as above, this will signify that when editing this User and determining unique entries, ignore the value in the email column where users.id = $u->id.

How to validate email as unique while updating data using laravel form request?

I will assume that you are posting a user object, where id is included in your $request data.

In case you are storing a new record and email needs to be available, you can use:

use Illuminate\Validation\Rule;
...
public function rules() {
return [
'name' => 'required|max:255|string',
'email' => ['required', 'email', 'max:255', Rule::unique('users')],
'restaurant' => 'required',
];
}

If you want to ignore your user->id, because you are updating your record and obviously the email is taken by the user:

use Illuminate\Validation\Rule;
...
public function rules() {
return [
'name' => 'required|max:255|string',
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($this->id)],
'restaurant' => 'required',
];
}

laravel 7 profile udpate wiith email unique:users

you can use update in place of create like this:

$user::update([
'name' => $validate['name'],
'email' => $validate['email'],
'password' => Hash::make($password),
]);


Related Topics



Leave a reply



Submit