2016-12-30 35 views
1

如何为下面的相同路由创建多个请求。删除一条记录给了我一个在laravel 5中的NotFoundException

Route.php

Route::get('/home', '[email protected]');//->middleware('auth'); 
Route::get('/home/{$user}','[email protected]'); 
Route::delete('/home/{$studentId}','[email protected]'); 

形式工作正常,直到我添加删除请求。在我的刀片模板中,我有这样的代码。

home.blade.php

<form class="" role="form" method="DELETE" action="/home/{{$student->id}}"> 
          {{ csrf_field() }} 
          <td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td> 
          </form> 

我相信,因为它显示NotFoundHTTPException在相同航线。

在一条路线/家我试图添加,显示,编辑和删除一个记录与不同的按钮。

在此先感谢。

回答

-1

HTML表单不支持getpost以外的方法。如果你需要进行模拟,包括隐藏的输入到模拟delete

<input name="_method" type="hidden" value="DELETE"> 

然后在你的代码,将其更新到:

<form class="" role="form" method="POST" action="/home/{{$student->id}}"> 
    {{ csrf_field() }} 
    <input name="_method" type="hidden" value="DELETE"> 
    <td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td> 
</form> 

参考:

+0

他要删除,但是......你刚从laravel网站上复制'<输入名称= “_method”type =“hidden”value =“PUT”>'。错误的答案。 –

+0

哇..这样的接受降级..你们是残酷的,永远不会给我们一个机会.. –

1

你可以添加一个形式和使用Laravel的形式方法欺骗

<input type="hidden" name="_method" value="DELETE"> 

参见更多... http://laravel.com/docs/master/routing#form-method-spoofing

如下尝试....

<form class="" role="form" method="DELETE" action="/home/{{$student->id}}"> 
<input type="hidden" name="_method" value="DELETE"> 
<input type="hidden" name="_token" value="{{ csrf_token() }}">   
<td><button type="submit" class="btn btn-primary pull-right">Remove Student</button></td> 
</form> 
+0

我已经尝试过,但没有运气。它仍然显示出我没有发现的异常。 – vsoni

+1

你的'资源控制器'..https:// laravel.com/docs/5.3/controllers#resource-controllers'用于CRUD操作...不需要为每个操作指定'routes' .. –

+0

非常感谢。我已经删除了所有的家庭控制器路径和添加 – vsoni

0

1)改变你路线来自:

Route::delete('/home/{$studentId}','[email protected]'); 

收件人:

Route::get('/delete/{$Id}','[email protected]')->name('delete'); 

2)改变你形成从标签:

<form class="" role="form" method="DELETE" action="/home/{{$student->id}}"> 

要:

<form class="" role="form" method="get" action="route('delete', ['id' => $student->id])"> 
相关问题