我对laravel还比较陌生,我想创建一个功能,通过单击链接从数据库中删除一条记录。
下面是视图上的代码:
@foreach($companies as $company)
<tr>
<td><a href="/companies/{{$company->id}}"> {{$company->name}}</a></td>
<td><a href="/companies/{{$company->id}}"> {{$company->descriptions}}</a></td>
<td><a href="/companies/{{$company->id}}"> {{$company->comptype->name}}</a></td>
<td>
<a href="{{route('companies.edit', $company->id)}}" class="btn btn-xs btn-default"><i class="fa fa-edit"></i></a>
<a onclick="
var result = confirm('Are you sure you wish to delete this Company?');
if( result ){
event.preventDefault();
document.getElementById('delete-form').submit();
}
"
class="btn btn-xs btn-default"><i class="fa fa-trash"></i> </a>
<form id="delete-form" action="{{ route('companies.destroy',[$company->id]) }}"
method="POST" style="display: none;">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
</form>
</td>
</tr>
@endforeach
接下来是控制器中的destroy方法
public function destroy(Company $company)
{
//
// dd($company);
$findCompany = Company::findOrFail( $company->id);
if($findCompany->delete()){
//redirect
return redirect()->route('companies.index')
->with('success' , 'Company deleted successfully');
}
return back()->withInput()->with('error' , 'Company could not be deleted');
}
表单如下所示:
当单击垃圾箱时,要删除一个特定的记录,表上的记录会被删除,不管我尝试删除哪个记录,第一个记录总是会被删除。当我做了一个diedown,我发现url有正确的记录,但dd显示第一个记录将被删除。我对laravel还比较陌生,我使用laravelcollective/html完成了这项工作,但不幸的是,它与laravel5.5.28不兼容。请帮忙
5条答案
按热度按时间shyt4zoc1#
试试看这个动作
"{{ route('companies.destroy',[$company->id]) }}"
至{{ url('companies/destroy/'.$company->id) }}
查看错误的销毁方法点击链接完美的方法
brgchamk2#
请为查看文件尝试以下代码:
slidercontroller.php:
kcwpcxri3#
试试这个:
xyhw6mcr4#
试试看,这是我的项目代码:
查看文件:
tagcontroller.php:
xzv2uavs5#
问题出在js代码中。您的js代码总是使用相同的delete表单。要解决此问题,您需要使用唯一ID:
然后:
而且,混合js和php是一种不好的做法。你真的应该把所有的js放到一个单独的js文件中。