php 如何在 Laravel 中使用补丁请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42313033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use patch request in Laravel?
提问by Darama
There is entity Userthat is stoted in table Users
有实体User是在表stotedUsers
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 PATCHmethod in Laravel:
为此,我尝试PATCH在 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 $idparameter to routing and controller relatively?
这是否意味着我可以传递任何数据进行更新?我应该$id相对地将参数传递给路由和控制器吗?
How to use right handler for PATCH method in Laravel?
如何在 Laravel 中为 PATCH 方法使用正确的处理程序?
回答by Jigneshsinh Rathod
your route is:
你的路线是:
Route::patch('users/update', 'UsersController@update');
replace your route with following route that use for all CRUD opration:
用以下用于所有 CRUD 操作的路线替换您的路线:
Route::resource('users', 'UsersController');
if you use ajax for submit data then replace your type and url with following:
如果您使用 ajax 提交数据,则将您的类型和 url 替换为以下内容:
type: "patch",
url: "{{url('/')}}users/" + id,
if you don't use ajax than use following:
如果您不使用ajax,请使用以下内容:
<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:
更新:在 5.6 版之后,您可以在任何刀片文件中对上述函数使用这些语法:
<form method="POST" action="{{route('users.update',['id' => $id])}}>
@csrf
@method('PATCH')
</form>
回答by Pramod Patil
Update the routing as per below
更新路由如下
Route::patch('/users/update/{id}',[
'uses' => 'UsersController@update'
]);
回答by aleksejjj
Yes, you need to send id for route patch. Example from https://laravel.com/docs/5.4/controllers#resource-controllersfor Laravel
是的,您需要发送 id 以获取路由补丁。来自https://laravel.com/docs/5.4/controllers#resource-controllers的 Laravel 示例
PUT/PATCH - /photos/{photo}, so you don't need updateword in your route. Just users/id and methods PUT or PATCH.
PUT/PATCH - /photos/{photo},因此您update的路线中不需要文字。只是用户/ID 和方法 PUT 或 PATCH。
UPD for CRUD operations:
用于 CRUD 操作的 UPD:
// 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
}

