Laravel路线资源销毁不起作用

Laravel路线资源销毁不起作用

这是我的表格:

<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>

这是我的 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.']);
}

但是它不能删除哪里出了问题?如何解决呢?

最佳答案

您需要对表单使用POST方法,并添加名称为input和值_methodDELETE元素。另外,添加 token :

<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>

关于php - Laravel路线资源销毁不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39916547/

10-16 22:06