24

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?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Darama
  • 3,130
  • 7
  • 25
  • 34
  • Could you run `php artisan route:list` and show us what it tells you about this specific route that you registered? – Marco Aurélio Deleu Feb 18 '17 at 08: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');` – Peter Reshetin Feb 18 '17 at 08:58

3 Answers3

59

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>
  • 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. – Amit Shah Jul 18 '18 at 13:56
  • 1
    @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: – sanders Dec 26 '18 at 09:28
4

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
}
aleksejjj
  • 1,715
  • 10
  • 21
3

Update the routing as per below

Route::patch('/users/update/{id}',[
    'uses' => 'UsersController@update'
]);
Pramod Patil
  • 2,704
  • 2
  • 14
  • 20