Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wizjo's avatar
Level 26

Update user e-mail to be unique (using Request class)

I have an account page where user can update his e-mail address. Now what I`m trying to do is to update it with checking if it is unique in users table, but with ignoring current user ID.

I red the documentation, but it doesn`t describe that situation (while using request class).

Here are my files:

UserController.php

    public function update(UserUpdateRequest $request) {
        $user = Auth::user();
        $user->update($request->toArray()); 

        Session::flash('message', 'The informations were successfully updated.');
        return Redirect::back();        
    }

UserUpdateRequest.php

    public function rules()
    {
        return [
        'email'  =>  'required|unique:users,email,'.$id,
       //other attributes...
        ];
    }

This gives me error:

Undefined variable: id

After changing validation rule to that:

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

while trying to update user and changing any other "attributes", but no "email" I got message that this e-mail is already taken so I think that it doesn`t see ID in validation rule. How to pass that id to rule?

0 likes
10 replies
kendrick's avatar

'email' => 'required|email|unique:users should do it in terms of Validation

Snapey's avatar

Like this

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

you will be getting back null from $this->id

change to

'email'  =>  'required|unique:users,email,'.$this->user()->id,
wizjo's avatar
Level 26

I noticed one problem: $this->user() returns current logged user, but if I want to edit another user as admin it is incorrect. I need to pass ID of current edited user instead.

Snapey's avatar

in which case the id should be in the route? You can get it from there, but need to check if the current user = subject being edited, or they are an admin.

In this case, your original $this->id would be the correct solution but maybe you don't have the resource id in the route?

Snapey's avatar

what is the parameter called in the route?

wizjo's avatar
Level 26

I use something like this:

Route::group(['middleware' => ['auth', 'web'], 'prefix' => 'admin'], function () {
    Route::resource('users', 'Backend\UserController');
});
Snapey's avatar
Snapey
Best Answer
Level 122

So the parameter in the URL (and you can confirm this with php artisan route:list) for a resource route is the singular form of the resource

so in this case the url for edit would be users/{user}/edit

so what does $request->user contain?

wizjo's avatar
Level 26

You are right $request->user contains only the ID of current edited user, so I can use it. I thought I should search for something with "id", but it seems that using default resource in routes cause that it uses model name. Now I understand everything. Thank you!

Please or to participate in this conversation.