我试图软删除我的文档模型,例如ID 106 。我已经在我的表单中声明了DELETE
方法也在我的路线中我定义Route::delete
函数也在我的文档模型中已经定义了use SoftDeletes;
但它给我一个 MethodNotAllowedHttpException 为什么我收到此错误的任何帮助?感谢您的提示或帮助:)
路线:
Route::get('/documents/pending',
[
'uses' => '\App\Http\Controllers\DocumentController@documentsSentForApproval',
'as' => 'document.pending',
]);
Route::delete('/documents/pending/{id}/hide',
[
'uses' => '\App\Http\Controllers\DocumentController@hideApprovalsDocument',
'as' => 'document.softDeletePending',
]);
控制器:
public function hideApprovalsDocument(Document $id)
{
$hide = Document::find($id);
$hide->delete();
return redirect()->back();
}
查看:
@foreach ($pendingDocuments as $list)
<tr class = "info">
<td>
<form class = "form-inline" role = "form" method = "DELETE" action = "/documents/pending/{{ $list->documentId }}/hide">
<div class = "form-group">
<a href = "{{ route ('document.softDeletePending', $list->documentId) }}"><button type = "submit" class = "btn btn-danger glyphicon glyphicon-eye-close"> Hide</button></a>
</div>
</form>
</td>
</tr>
@endforeach
堆栈跟踪:
in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('DELETE')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('DELETE')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 823
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
答案 0 :(得分:2)
您需要一个隐藏的_method
输入字段,声明删除请求
表单没有PUT
,PATCH
或DELETE
,因为表单只能处理GET和POST请求
为了让Laravel处理DELETE
请求,您必须传递
<input type="hidden" name="_method" value="DELETE">
并且Laravel会识别您的DELETE
请求
答案 1 :(得分:1)
HTML表单不支持PUT,PATCH或DELETE操作。因此,在定义从HTML表单调用的PUT,PATCH或DELETE路由时,您需要向表单添加隐藏的_method字段。使用_method字段发送的值将用作HTTP请求方法:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
您可以使用method_field助手生成_method输入:
{{ method_field('DELETE') }}
答案 2 :(得分:1)
已经通过在视图中获取hidden input
并在Controller中使用它来解决问题。
查看:
<form class = "form-inline" role = "form" method = "POST" action = "/documents/pending/{{ $list->documentId }}/hide">
{{ method_field('PATCH') }}
<input type = "hidden" name = "softDelete" value = "{{ $list->documentId }}">
<div class = "form-group">
<a href = "{{ route ('document.softDeletePending', $list->documentId) }}"><button type = "submit" class = "btn btn-danger glyphicon glyphicon-eye-close"> Hide</button></a>
</div>
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">
</form>
控制器:
public function hideApprovalsDocument(Request $request, Document $id)
{
//Getting the request in the View.
$id = $request->get('softDelete');
$hide = Document::where('id', '=', $id);
$hide->delete();
return redirect()->back();
}