删除单记录laravel

af7jpaap  于 2021-06-21  发布在  Mysql
关注(0)|答案(5)|浏览(323)

我对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不兼容。请帮忙

shyt4zoc

shyt4zoc1#

试试看这个动作 "{{ route('companies.destroy',[$company->id]) }}"{{ url('companies/destroy/'.$company->id) }} 查看错误的销毁方法点击链接
完美的方法

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');}
brgchamk

brgchamk2#

请为查看文件尝试以下代码:

<div class="col-xs-12">
<a href="{{url('/delete')}}/{{$allDemos[$i]->id}}"  class='btndelete'>
     <i class="fa fa-trash"></i>
</a>

slidercontroller.php:

public function delete($id){
      $Slider = Slider::find($id); 
      unlink('slider_images/'.$Slider->slider_img);   
      $Slider->delete();
      return redirect('/slider');
 }
kcwpcxri

kcwpcxri3#

试试这个:

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');
}
xyhw6mcr

xyhw6mcr4#

试试看,这是我的项目代码:
查看文件:

<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...');
    }
xzv2uavs

xzv2uavs5#

问题出在js代码中。您的js代码总是使用相同的delete表单。要解决此问题,您需要使用唯一ID:

<form id="delete-form{{ $company->id }}"

然后:

document.getElementById('delete-form{{ $company->id }}').submit();

而且,混合js和php是一种不好的做法。你真的应该把所有的js放到一个单独的js文件中。

相关问题