我的控制器中包含以下代码:
public function update(Request $request, $id)
{
$cook = cooks::findOrFail($id);
if (!$cook) {
return response()->json([
'success' => false,
'message' => 'Sorry, cook with id ' . $id . ' cannot be found',
], 400);
}
$updated = $cook->fill($request->all())
->save();
if ($updated) {
return response()->json([
'success' => true,
]);
} else {
return response()->json([
'success' => false,
'message' => 'Sorry, cook could not be updated',
], 500);
}
}
当我在邮递员方法PUT中使用时,我收到此消息“此路由不支持PUT方法。受支持的方法:GET,HEAD,POST。”
在我的api.php中,我有以下一行:
Route :: resource('cook','cookList');
这是我在邮递员中使用的路线:
`http://127.0.0.1:8000/api/cook`
具有主体ID-1和标题的厨师 有人可以帮我吗?
答案 0 :(得分:1)
您在路线中缺少参数。在使用http://127.0.0.1:8000/api/cook
时,猜测您正在尝试使用index
方法或store
方法。因此,向您的路由添加一个id参数,它应该可以与PUT
方法一起使用。
http://127.0.0.1:8000/api/cook/1
(未经测试,但应该可以使用)
答案 1 :(得分:0)
您可以尝试使用表单方法欺骗: https://laravel.com/docs/5.8/routing#form-method-spoofing