Laravel Route 资源销毁不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39916547/
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 Route resource destroy not working
提问by Nazmul Hossain
Here is my form :
这是我的表格:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
<input type="hidden" name="_method" value="DELETE" />
</div>
</form>
Here is my controller :
这是我的控制器:
public function destroy($id)
{
$invoice = Invoice::find($id);
if(!$invoice){
return redirect()->route('invoice.index')->with(['fail' => 'Page not found !']);
}
$invoice->delete();
return redirect()->route('invoice.index')->with(['success' => 'Invoice Deleted.']);
}
But it can not delete where is the problem ? How solve this ?
但是不能删除,问题出在哪里?这个怎么解决?
回答by Alexey Mezenin
You need to use POST
method for the form and add input
element with name _method
and value DELETE
. Also, add token:
您需要POST
为表单使用method 并添加input
具有 name_method
和 value 的元素 DELETE
。另外,添加令牌:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
回答by Mihai Matei
I think you must add a hidden input to the form which will contain the method used:
我认为您必须在表单中添加一个隐藏输入,其中将包含所使用的方法:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input type="hidden" name="_method" value="DELETE" />
</form>
Read more on Laravel documentation about Form method spoofing
阅读有关表单方法欺骗的Laravel 文档的更多信息
回答by Eran Machiels
In order to get PUT
and DELETE
methods to work, you need an additional field, since only POST
and GET
are possible within HTML (out-of-the-box).
为了让 getPUT
和DELETE
methods 起作用,您需要一个额外的字段,因为只有POST
和GET
可以在 HTML 中使用(开箱即用)。
The additional field will be made with the code:
附加字段将使用以下代码:
{!! method_field('DELETE') !!}
So your form will look like this:
因此,您的表单将如下所示:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
{!! method_field('DELETE') !!}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
回答by user11579861
Also, if you are using blade templates, you can add the method field like so: @method('DELETE')
此外,如果您使用刀片模板,您可以像这样添加方法字段:@method('DELETE')
回答by Waheed Shahzad
More Laravel way you can do this
更多的 Laravel 方式,你可以做到这一点
<form action="{{ route('invoice.destroy',$invoice->id)}}" method="POST">
@method('DELETE')
<button type="submit" class="btn btn-primary">Yes</button>
</form>