How to use patch request in Laravel?

冷暖自知 提交于 2019-12-03 02:25:07

your route is:

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

replace your route with following route that use for all CRUD opration:

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

if you use ajax for submit data then replace your type and url with following:

type: "patch",
url: "{{url('/')}}users/" + id,

if you don't use ajax than use following:

<form method="POST" action="{{route('users.update',['id' => $id])}}">
    {{csrf_field()}}
    {{ method_field('PATCH') }}
</form>

update: after version 5.6 you can use these syntax for above functions in any blade file:

<form method="POST" action="{{route('users.update',['id' => $id])}}>
    @csrf
    @method('PATCH')
</form>

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
}

Update the routing as per below

Route::patch('/users/update/{id}',[
    'uses' => 'UsersController@update'
]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!