laravel 删除表单无效
laravel delete form is not working
@extends('layouts.app')
@section('content')
<div class="row">
<div style="float:left;" class="col-md-8">
<h3>Currency</h3>
</div>
<div style="float:right;" class="col-md-4">
<a href="/currency/create" class="btn btn-dark btn-sm ml-3" style="float: right"><i class="fa fa-edit" style="color:white;"></i>Add New</a>
<input type="text" id="search" name="search" placeholder="search" class="form-control-sm col-md-8 ml-1" style="float:right;"><br><br>
</div>
</div>
@if(count($currencies) > 0 )
<table class="table table-striped table-sm" >
<thead>
<tr><th scope="col">ID</th><th scope="col">Currency</th><th scope="col">Country</th><th scope="col" colspan="2" style="text-align:right">Actions</th></tr>
</thead>
@foreach($currencies as $currency)
<tr><td scope="row">{{$currency->id}}</td>
<td>{{$currency->title}}</td>
<td>{{$currency->country}}</td>
{!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
{{Form::hidden('_method','DELETE')}}
@csrf
<td><button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>
<a href="/currency/{{$currency->id}}/edit" class="btn btn-primary btn-sm" style="float:right" ><i class="fa fa-edit" style="color:white;"></i>Edit</a></td></tr>
{!!Form::close() !!}
@endforeach
</table>
{{$currencies->links()}}
@else
<p> No Data Available </p>
@endif
@endsection
大家好,删除表单没有在此代码中提交,我尝试了很多方法但我无法找出问题所在。请帮我解决一下这个。
在我看来,您的 html 是错误的,您打开、关闭表单并将 @csrf
放在 td
标签之外,而提交按钮在其中
尝试将表单定义移动到 td
标记内,例如:
<td>
{!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
{{Form::hidden('_method','DELETE')}}
@csrf
<button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>
<a href="/currency/{{$currency->id}}/edit" class="btn btn-primary btn-sm" style="float:right" ><i class="fa fa-edit" style="color:white;"></i>Edit</a>
{!!Form::close() !!}
</td>
尝试将实际 form
标记中的方法更改为 DELETE?默认情况下,如果您在路由中使用 route::resource
之类的东西,它会自动将 DELETE 方法分配给路由,而不是 POST.
我看得出您在隐藏元素中指定了 delete 方法,但值得一试。
{!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
如果您不太确定,请在项目根目录中的终端中 运行 php artisan route:list
,它会为您提供应用程序的路由和方法的完整列表。
@extends('layouts.app')
@section('content')
<div class="row">
<div style="float:left;" class="col-md-8">
<h3>Currency</h3>
</div>
<div style="float:right;" class="col-md-4">
<a href="/currency/create" class="btn btn-dark btn-sm ml-3" style="float: right"><i class="fa fa-edit" style="color:white;"></i>Add New</a>
<input type="text" id="search" name="search" placeholder="search" class="form-control-sm col-md-8 ml-1" style="float:right;"><br><br>
</div>
</div>
@if(count($currencies) > 0 )
<table class="table table-striped table-sm" >
<thead>
<tr><th scope="col">ID</th><th scope="col">Currency</th><th scope="col">Country</th><th scope="col" colspan="2" style="text-align:right">Actions</th></tr>
</thead>
@foreach($currencies as $currency)
<tr><td scope="row">{{$currency->id}}</td>
<td>{{$currency->title}}</td>
<td>{{$currency->country}}</td>
{!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
{{Form::hidden('_method','DELETE')}}
@csrf
<td><button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>
<a href="/currency/{{$currency->id}}/edit" class="btn btn-primary btn-sm" style="float:right" ><i class="fa fa-edit" style="color:white;"></i>Edit</a></td></tr>
{!!Form::close() !!}
@endforeach
</table>
{{$currencies->links()}}
@else
<p> No Data Available </p>
@endif
@endsection
大家好,删除表单没有在此代码中提交,我尝试了很多方法但我无法找出问题所在。请帮我解决一下这个。
在我看来,您的 html 是错误的,您打开、关闭表单并将 @csrf
放在 td
标签之外,而提交按钮在其中
尝试将表单定义移动到 td
标记内,例如:
<td>
{!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
{{Form::hidden('_method','DELETE')}}
@csrf
<button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>
<a href="/currency/{{$currency->id}}/edit" class="btn btn-primary btn-sm" style="float:right" ><i class="fa fa-edit" style="color:white;"></i>Edit</a>
{!!Form::close() !!}
</td>
尝试将实际 form
标记中的方法更改为 DELETE?默认情况下,如果您在路由中使用 route::resource
之类的东西,它会自动将 DELETE 方法分配给路由,而不是 POST.
我看得出您在隐藏元素中指定了 delete 方法,但值得一试。
{!!Form::open(['action'=>['CurrencyController@destroy', $currency->id], 'method'=>'POST'])!!}
如果您不太确定,请在项目根目录中的终端中 运行 php artisan route:list
,它会为您提供应用程序的路由和方法的完整列表。