根据复选框从数据库中删除多行

时间:2015-01-20 08:32:46

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

我在从数据库中删除时遇到问题。例如,如果我检查了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包含所有选中的项目,但是当我按下删除按钮时,只删除了一个项目并未全部选中。 请帮帮我。

2 个答案:

答案 0 :(得分: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();
    }

答案 1 :(得分:0)

你试过了吗?

$aPhotoChecked = Input::get('aObjects');

foreach($aPhotoChecked as $key => $n ) 
{
    $oPhoto = \Photo::find( $aPhotoChecked[$key] );
    .....
    // you can dump here and debug your arrays :)
}   
相关问题