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

XSkinner's avatar

Route::resource, unique rule and PATCH Method

Hello.

I'm making a system which allows to edit a user, i have been using per route Routes schema, and today I changed to Route::resources which works pretty well with all the methods except one... PATCH (update). When i'm updating an user, it returns error for all the fields which have unique vlidation

public function rules()
{
    return [
        'username' => 'required|min:3|max:255|alpha_num|unique:users,username,'.$this->id,
        'name' => 'required|min:3|max:255',
        'email' => 'required|email|max:255|unique:users,email,'.$this->id,
        'role_id' => 'required|integer'
    ];
}

Here are my rules in the UserRequest which work fine for create and update when sending through POST method, but if i use PATCH it does not take the $this->id in the rule... i know it because if i harcode the user_id like this

 'email' => 'required|email|max:255|unique:users,email,15',

For the user with id 15, it works very well, why it is not when using patch?

EDIT: i tried

dd($this->id);

When sending using post and patch and for post is the actual user id, but for PATCH it returns null

Here are my create and edit forms

http://laravel.io/bin/BLGeN

0 likes
7 replies
usman's avatar

Html forms do not support REST methods like put, patch and delete. You will need to spoof the form using a hidden field :

 <input type="hidden" name="_method" value="PUT">
//or using the helper
{!! method_field('PUT'); !!}

Form method spoofing is pretty succinctly explained in the documentation: http://laravel.com/docs/5.1/routing#form-method-spoofing ,

Usman.

XSkinner's avatar

Yeah @usman as i posted in the bin... Here is my form

{!! Form::model($user, ['method' => 'PATCH', 'action' => ['Admin\UsersController@update', $user->id]]) !!}

Which parse a hidden input field with the method set to patch.

By the way, the request is made using patch but the problem is there is not a $this->id

usman's avatar

Ahh I missed the link in the bottom. @XSkinner are you sure the hidden field is present in the markup.

XSkinner's avatar

Yeah i'm sure, it is.

It is even sent to the server as _method = PATCH

usman's avatar
usman
Best Answer
Level 27

@XSkinner the FormRequest retrieves the id from the route parameters. As you have mentioned that you are using the resource controllers they generate a little different routes and use the name of the resource as the parameter. That said your id is now named as user if the name of the resource your controller is referring to is a User i.e. UsersController.

// as an example the route:
/user/{id}/edit
// will be changed as:
/user/{user}/edit
//so now you need to look for the $this->user instead of $this->id.

You can run the artisan route:list command and replace the id with the parameter you see in the routes list table.

Usman.

Please or to participate in this conversation.