我对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
接下来是Controller中的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');
}
当点击垃圾箱,删除特定记录时,表上的记录会被删除,我尝试删除的内容并不重要,第一条记录总是会被删除。当我做了一个死机时,我发现URL有正确的记录,但是dd显示第一条记录将被删除。我是Laravel的新手,我使用Laravelcollective / html完成了这项工作,但不幸的是它与Laravel 5.5.28不兼容。请帮助
答案 0 :(得分:3)
问题出在JS代码中。您的JS代码总是相同的删除形式。要解决此问题,您需要使用唯一ID:
<form id="delete-form{{ $company->id }}"
然后:
document.getElementById('delete-form{{ $company->id }}').submit();
此外,混合JS和PHP是一种不好的做法。你应该把所有的JS放到一个单独的JS文件中。
答案 1 :(得分:1)
试试这是我的项目代码:
查看文件:
<td>
<form action="{{action('TagController@destroy', $row['id'])}}" method="post">
{{csrf_field()}}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
TagController.php:
public function destroy($id)
{
$tags = Tag::find($id);
$success = $tags->delete();
if($success)
{
return redirect('/admin/tag')
->with('success','Tag deleted.');
}
return back()->withInput()->with('errors','Some error...');
}
答案 2 :(得分:1)
试试这个
请参阅表单操作"{{ route('companies.destroy',[$company->id]) }}"
至{{ url('companies/destroy/'.$company->id) }}
看到毁灭方法错了
点击link
完美方法
public function destroy($id){
$findCompany = Company::findOrFail($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');}
答案 3 :(得分:1)
尝试使用此代码查看视图文件:
<div class="col-xs-12">
<a href="{{url('/delete')}}/{{$allDemos[$i]->id}}" class='btndelete'>
<i class="fa fa-trash"></i>
</a>
SliderControler.php:
public function delete($id){
$Slider = Slider::find($id);
unlink('slider_images/'.$Slider->slider_img);
$Slider->delete();
return redirect('/slider');
}
答案 4 :(得分:0)
试试这个:
public function destroy(Company $company)
{
if($company->delete()){
//redirect
return redirect()->route('companies.index')
->with('success' , 'Company deleted successfully');
}
return back()->withInput()->with('error' , 'Company could not be deleted');
}