Laravel - DELETE 方法不支持删除路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55771840/
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
Laravel - DELETE method is not support for a delete route
提问by Bernard Polman
I'm a complete beginner at laravel and am currently making a simple admin panel. I have a grid that shows users (name, email, etc...) and the problem I have is probably stupid but I can't figure it out. I created a controller method for deleting a user:
我是 laravel 的完全初学者,目前正在制作一个简单的管理面板。我有一个显示用户(姓名、电子邮件等)的网格,我遇到的问题可能很愚蠢,但我无法弄清楚。我创建了一个用于删除用户的控制器方法:
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return redirect('/admin')->with('success', 'User has been deleted');
}
And defined route as this:
并将路线定义为:
Route::post('/admin/delete/{id}', 'AdminController@destroy')
->middleware('is_admin')
->name('admin.destroy');
and to delete a user in grid, I used form in my view and even setup headers:
为了删除网格中的用户,我在视图中使用了表单,甚至设置了标题:
<td>
<form href="{{ route('admin.destroy', $user->id)}}" method="post">
@method('DELETE')
@csrf
<input class="btn btn-danger" type="submit" value="Delete" />
</form>
And everytime I press button for deleting a user, I get this:
每次我按下按钮删除用户时,我都会得到以下信息:
The DELETE method is not supported for this route. Supported methods: GET, HEAD.
I just can't figure out what I'm doing wrong. I tried changing route type to post but I get the same error.
我只是想不通我做错了什么。我尝试将路由类型更改为发布,但出现相同的错误。
回答by PtrTon
Your form does not contain an action
, so it will submit it to the same url as it's on, which is only GET/HEAD.
您的表单不包含action
,因此它会将其提交到与它所在的网址相同的网址,即只有 GET/HEAD。
Try this instead:
试试这个:
<form action="{{ route('admin.destroy', $user->id)}}" method="post">
@method('DELETE')
@csrf
<input class="btn btn-danger" type="submit" value="Delete" />
</form>