【问题标题】:Delete multiple rows from database based on the checkboxes根据复选框从数据库中删除多行
【发布时间】:2015-03-18 09:50:38
【问题描述】:

我在从数据库中删除时遇到问题。例如,如果我检查了 5 项,则只有一项被删除。 Normaly 脚本是正确的: 我的看法:

{{ Form::open(array('url'=>'/administration/photo/deletePhoto','method' => 'post','files'=>true)) }}
            @foreach($aPhotoInCategory as $photo)
                {{ HTML::image($photo->name,'alt', array('class'=>'news-content-image','width' => 95, 'height' => 70 )) }}
                {{ Form::checkbox('aObjects[]',$photo->id)}}
                {{ Form::hidden('id',$aOnePhotoCategory['id']) }}
            @endforeach
        <br/><br/>
{{ Form::submit('Delete',array('class'=>'btn btn-primary')) }}
{{ Form::close() }}

我的控制器:

public function deletePhoto(){
    $aPhotoChecked = Input::get('aObjects');
    $id            = Input::get('id');
    foreach($aPhotoChecked as $photo){
        $oPhoto = \Photo::find($photo);
        if($oPhoto){
            File::delete('public/'.$oPhoto->name);
            $oPhoto->delete();
            return Redirect::to('administration/category_photo/edit/'.$id)
                   ->with('message_succes','Succes');
        }
    }
}

数组 $aPhotoChecked 包含所有检查的项目,但是当我按下删除按钮时,只有一个项目被删除,而不是所有检查。 请帮帮我。

【问题讨论】:

    标签: laravel laravel-4 laravel-routing laravel-3 laravel-5


    【解决方案1】:

    我用 for 解决了这个问题:

    for($i=0;$i<count($aPhotoChecked);$i++){
            $oPhoto = \Photo::find($aPhotoChecked[$i]);
            Log::info(print_r(public_path(),true));
            File::delete(public_path().'/'.$oPhoto->name);
            DB::table('photo')->where('id', '=', $aPhotoChecked[$i])->delete();
        }
    

    【讨论】:

      【解决方案2】:

      你试过了吗:

      $aPhotoChecked = Input::get('aObjects');
      
      foreach($aPhotoChecked as $key => $n ) 
      {
          $oPhoto = \Photo::find( $aPhotoChecked[$key] );
          .....
          // you can dump here and debug your arrays :)
      }   
      

      【讨论】:

        猜你喜欢
        • 2015-11-13
        • 1970-01-01
        • 2017-08-19
        • 1970-01-01
        • 2018-03-09
        • 2014-05-27
        • 1970-01-01
        • 1970-01-01
        • 2012-02-10
        相关资源
        最近更新 更多