Fireynis
Fireynis

Reputation: 447

Admin user change with unique email contraint

I am using the unique rule on a request to ensure no duplicate emails are entered.

Looking at Laravel docs I saw how to ignore the posting users email, but what about an Admin who has the privilege of making an making a change to a user. I still want to check for uniqueness but skip the email of the user being modified not the admins email.

Any way I can do that?

Upvotes: 0

Views: 86

Answers (1)

plmrlnsnts
plmrlnsnts

Reputation: 1684

You just have to pass the id of the user you are trying to modify.

Let's say in your controller you have

public function update(UserRequest $request, $id) {
    ...
}

Then in your UserRequest you can retrieve the route parameter $id using

public function rules() {
    $id = $this->route('id') ?: 0;

    return [
        'email' => "required|unique:users,email,{$id}",
    ];
}

Upvotes: 1

Related Questions