28

There is entity User that is stoted in table Users

Some fields in this table are null by default.

I need to update these fields and set not null data.

For this I try to use PATCH method in Laravel:

Routing:

Route::patch('users/update', 'UsersController@update');

Controller:

public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            "name" => 'required|string|min:3|max:50',
            "email_work" => 'email|max:255|unique:users',
            "surname" => 'required|string|min:3|max:50',
            "tel" => 'required|numeric|size:11',
            "country" => 'required|integer',
            "region" => 'required|integer',
            "city" => 'required|integer'
        ]);

        if ($validator->fails()) {
            return response()->json(["message" => $validator->errors()->all()], 400);
        }

        $user = User::where("user_id", $id)->update([
            "name" => $request->name,
            "surname" => $request->surname,
            "tel" => $request->tel,
            "country" => $request->country,
            "city" => $request->city,
            "region" => $request->region,
            "email_work" => $request->email
        ]);

        return response()->json(["user" => $user]);

    }

Does it mean that I can pass any data to update? Should I pass $id parameter to routing and controller relatively?

How to use right handler for PATCH method in Laravel?

2
  • Could you run php artisan route:list and show us what it tells you about this specific route that you registered? Commented Feb 18, 2017 at 8:55
  • 1
    if you have $id parameter in update() function, then i suppose you need to add {id} your route, like this Route::patch('users/{id}/update', 'UsersController@update'); Commented Feb 18, 2017 at 8:58

3 Answers 3

61

Using single route :

Route::patch('users/{id}', 'UsersController@update')->name('users.update');

Or Using resource route :

Route::resource('users', 'UsersController');

If you want use in jQuery.ajax():

$.ajax({
    method: "post",
    url: "{{ url('/users/') }}" + id,
    data: {"_method": "PATCH", ...}
    ...
});

If you want use in html form:

<form method="POST" action="{{ route('users.update',['id' => $id]) }}">
    @csrf
    @method('PATCH')
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

then when can we use method="PATCH" in Laravel blade forms? I just noticed it's not working for me too if I use Patch method in form.
@AmitShah from the Laravel documentation: HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
5

Yes, you need to send id for route patch. Example from https://laravel.com/docs/5.4/controllers#resource-controllers for Laravel

PUT/PATCH - /photos/{photo}, so you don't need update word in your route. Just users/id and methods PUT or PATCH.

UPD for CRUD operations:

// Routes
Route::resource('items', 'ItemsController');

// Form for update item with id=1
<form method="POST" action="{{ route('items.update', ['id' => 1])}}">
    {!! csrf_field() !!}
    <input name="_method" type="hidden" value="PATCH">
    <!-- Your fields here -->
</form>

// Controller
public function update($id, Request $request)
{
    // Validation here

    $item = Item::findOrFail($id);

    // Update here
}

2 Comments

Could you share a real sample?
Thank you Man! It helps me to set multiple parameters to update form action.
3

Update the routing as per below

Route::patch('/users/update/{id}',[
    'uses' => 'UsersController@update'
]);

1 Comment

In my case I get id in controller from Auth object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.