【问题标题】:Delete single record Laravel删除单条记录 Laravel
【发布时间】:2018-06-11 22:29:49
【问题描述】:

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

表格如下所示:

单击垃圾箱时,要删除特定记录,表上的记录会被删除,无论我尝试删除哪条记录,第一条记录都会被删除。当我做 diedown 时,我发现 URL 有正确的记录,但 dd 显示第一条记录将被删除。我对 Laravel 相当陌生,我使用 Laravelcollective/html 完成了这项工作,但不幸的是它与 Laravel 5.5.28 不兼容。请热心帮忙

【问题讨论】:

  • 您是否在控制器中打印了 $company->id ?你每次都做对了吗?

标签: php mysql laravel


【解决方案1】:

问题出在 JS 代码中。您的 JS 代码始终包含相同的删除表单。要解决此问题,您需要使用唯一 ID:

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

然后:

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

另外,混合使用 JS 和 PHP 是一种不好的做法。你真的应该把所有的JS放到一个单独的JS文件中。

【讨论】:

    【解决方案2】:

    试试这个代码查看文件:

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

    【讨论】:

      【解决方案3】:

      试试这个 请参阅表单操作"{{ route('companies.destroy',[$company-&gt;id]) }}"{{ url('companies/destroy/'.$company-&gt;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');}
      

      【讨论】:

        【解决方案4】:

        试试这样,这是我的项目代码:

        查看文件:

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

        【讨论】:

          【解决方案5】:

          试试这个:

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

          【讨论】:

            猜你喜欢
            • 2016-08-10
            • 2016-06-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-19
            • 2018-11-08
            • 2016-02-14
            相关资源
            最近更新 更多