Laravel 5.3 删除方法给出带有欺骗的 methodNotAllowed

Laravel 5.3 delete method gives methodNotAllowed with spoofing

我正在尝试使用 Laravel 的 (5.3) destroy 方法在我按下按钮时删除对象,但是我收到了 MethodNotAllowedHttpException。

我的路线是

Route::delete('/admin/add/{id}', 'ItemController@destroy');

销毁方法是

public function destroy($id)
{
    $items = Item::where('id', $id)->get();
    foreach ($items as $item)
        $item->delete();
    return redirect('/admin/add');
}

我的表格是

<form action="{{ url('admin/add/'.$specific->id)}}" method="POST">
  {{ method_field('DELETE') }}
  {{ csrf_field() }}
  <div class="modal-footer no-border">
    <button type="submit" class="btn btn-primary">Delete</button>
  </div>
</form>

我检查了现有的路线(php artisan route:list),删除的路线显示:

DELETE   | admin/add/{id}   |          | App\Http\Controllers\ItemController@destroy

我得到的错误是

in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 766
at Router->findRoute(object(Request)) in Router.php line 621
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 53
at require('/Users/username/Sites/blades/public/index.php') in server.php line 133

根据 Laravel 路由说明,我正在使用表单并使用 POST 方法欺骗 DELETE 操作,但我仍然遇到异常。任何帮助将不胜感激。

注意:我遵循了 https://laravel.com/docs/5.2/quickstart-intermediate 中的完整示例,它在那里工作得很好。我试图在不同的项目中重新创建它是导致问题的原因。

请检查$specific->id不为空,如dd($specific->id),请在浏览器打开开发者控制台并打开网络页面后,检查url是否正确。

我发现了问题。这个表单所在的页面上面有一个不同的 POST 表单,我用 </div> 而不是 </form> 标签关闭了表单。因此,它试图同时提交两个表单,并且 POST 调用发生冲突。