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

Qlic's avatar
Level 18

userRequest add and edit differences

Hi guys,

In my backend i can add and edit users, when adding a user the password and email are both required fields, where the email is also unique.

Works fine for adding, but i can't figure out how to get it to work with editing, since when i edit a user he is allowed to have the password field empty, and is allowed to use the e-mail address he already has.

However upon submitting i get the message that the e-mail is already in use.

use App\Http\Requests\Request;

class UserRequest extends Request {

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    $rules =  [
        'name' => 'required|min:3|max:25',
        'password' => 'required|min:5|max:25',
        'email' => 'required|email|unique:users',
        'active' => 'required',
        'type' => 'required'
    ];

    if ($this->method() == 'PATCH')
    {
        $rules['password'] = 'max:25';
        $rules['email'] = 'required|email|unique:users,email,'. $this->get('id');
    }

    return $rules;
}

}

0 likes
33 replies
toniperic's avatar

You could have two request objects - CreateUserRequest and EditUserRequest, as essentially those are the two different requests.

Qlic's avatar
Level 18

True, but even if i were to make two seperate request files, i would have to get the right condition for the e-mail check inside the edit request.

code_chris's avatar

I spent ages on a similar problem, try replacing this:

    $rules['email'] = 'required|email|unique:users,email,'. $this->get('id');

with:

        $rules['email'] = 'required|email|unique:users,email,'. $this->users;

I'm not sure why I ended up with this but it seemed to work for me, you can test it by doing dd($this->users); For me it returns the id.

$this->get('id'); returns null for me, maybe I did something wrong elsewhere?

Oh no I get it that's just the name of the wildcard in the url right? So if you're using route::resource then it will be the name of the table "users"

toniperic's avatar

Yeah, so what's wrong with that? An email has to be an email? You can pre-populate such fields with existing user data from the database.

Qlic's avatar
Level 18

@code_chris i tried your solution but i still reciece the same message, the e-mail address is already in use. And in a way that is correct, it is in use, but since i'm editing the user to which it belongs it should be allowed :P

@toniperic Yes, an e-mail has to be a valid e-mail type, it should also be unique. When editing the user through the form it might alter or it might remain the same. But what i can't figure out is how to make it unique, with the exeption that if the user being edited is the owner of that e-mail address it's allowed.

bestmomo's avatar
Level 52

If parameter name is id in route just do :

$rules['email'] = 'required|email|unique:users,email,'. $this->id;

If you have a resource would be :

$rules['email'] = 'required|email|unique:users,email,'. $this->user;

Also you can use this syntax for the verb :

if($this->isMethod('patch'))
code_chris's avatar

Make sure you are doing PATCH from your form and not PUT, just in case.

code_chris's avatar

@Qlic Make sure it actually outputs the id, so at the top of your rules() function:

public function rules()
{
    dd($this->users);
    //or $this->id
}

Make sure you are getting the id, otherwise its never going to work

code_chris's avatar

For example my route is: projects/{projects}

so using $this->projects gives me the id

You need to use whatever the wildcard is in the route you are patching to

Qlic's avatar
Level 18

my route is: Route::patch('admin/user/{id}', 'Admin\UserController@update');

toniperic's avatar

@code_chris think you don't have to give it just the ID. You can give the User object and it would still work.

code_chris's avatar

@toniperic Probably true, I'm just going with what works for me :)

@Qlic That should work with $this->id then, not sure why that wouldn't dump the id out..

Assuming you have the id in the route of your form when doing the patch request?

toniperic's avatar

If that's the case, then you could just default to

'email' => 'unique:users,email_address,' . Auth::user()

meaning you can always edit just your own data?

Qlic's avatar
Level 18

If i were only editing my own data, than yes. But in this case the admin can edit all users. This is my form header:

toniperic's avatar

How about

if(!$user->isAdmin()){
    // if user is not admin, return this $rules array
    return ['email' => 'unique:users,email_address,' . Auth::user()];
}

// user is admin
return ['email' => 'whatever'];
Qlic's avatar
Level 18

That might indeed work for the frontend part of the site where the user is editing it's own data. But currently i'm working on the backend, where the admin can edit all users.

So if my admin account whould have id 1, and the user i'm editing has id 2, that method would not work i assume?

Basically what i need to find out is why the ID from the url is not available in the request

code_chris's avatar

What was your Form::open or Form::model bit? Didn't see it above

Qlic's avatar
Level 18

@code_chris {!! Form::model($user, ['method' => 'PATCH', 'url' => 'admin/user/'. $user->id]) !!}

code_chris's avatar

@Qlic When you submit the form does it show the correct id in the address bar? Or if you use the network inspector in chrome to check it

Qlic's avatar
Level 18

Yes it shows the correct id in the url

code_chris's avatar

Running out of ideas, because if it's sending the id through and the route is using the wildcard {id} then doing $this->id in your request should output the id...

You could also try:

$this->segment(3);

See what that outputs

Qlic's avatar
Level 18

Well, the $this->segment(3); seems to work, it responds with "2".

However, out of curiosity i tried dd($this->id); and that also responds with "2".

code_chris's avatar

Well there you go then, you can use $this->id and it should work fine, how come it was null before?

Qlic's avatar
Level 18

No clue, when using the $this->get('id') you guys provided i got null, using $this->id gives me the correct id.

code_chris's avatar

I've never used $this->get('id'), but when I try it that doesn't work for me either. Looks like you have to add the id as a hidden field in the form for that to work.

Anyway $this->wildcard seems to work fine where wildcard is the {wildcard} from the route

1 like
bestmomo's avatar

When you use $this->get('id') you look in request inputs, not in url elements.

Next

Please or to participate in this conversation.