删除Multime Row Laravel

时间:2017-01-11 11:42:43

标签: php laravel laravel-5 laravel-5.1

在那里我有一个数据,我想在我选择的地方添加删除数据。 在这里我的代码:

public function postDeleteall(){
        $id = Request::get('id');
        if($id ==''){
            Session::flash('delete', 'select first !');
            return redirect()->back();
        }else{
            foreach ($id as $q) {
                $qwery = DB::table('log_patrols')->where('id',$q)->get();
                foreach ($qwery as $a) {
                    DB::table('log_patrols')->where('id',$a->id)->delete();
                    $row =DB::table('log_patrol_details')->where('id_log_patrols',$a->id)->get();
                    foreach ($row as $qwe) {
                        @unlink(public_path("uploads/".$qwe->photo1));
                        @unlink(public_path("uploads/".$qwe->photo2));
                        @unlink(public_path("uploads/".$qwe->photo3));
                    }

                    $rows = DB::table('log_patrol_details')->where('id_log_patrols',$q)->delete();
                    Session::flash('delete', 'success');
                    return redirect()->back();
                }

            }
        }
    }

当我尝试打印数据$id

here

它希望获取我选择的数据ID,但在我尝试制作代码之后,它的工作就像我想要的那样。并且它只删除我选择的1个数据。 我该怎么办 ?我需要改变哪些代码?

感谢

2 个答案:

答案 0 :(得分:0)

删除第一次巡逻后,您将重定向。移动

Session::flash('delete', 'success');
return redirect()->back();

foreach ($id as $q)循环之外。

答案 1 :(得分:0)

public function postDeleteall()
        {
            $id = Request::get('id');
            if ($id == '') {
                Session::flash('delete', 'select first !');
                return redirect()->back();
            } else {
                foreach ($id as $q) {
                    $qwery = DB::table('log_patrols')->where('id', $q)->get();
                    foreach ($qwery as $a) {
                        DB::table('log_patrols')->where('id', $a->id)->delete();
                        $row = DB::table('log_patrol_details')->where('id_log_patrols', $a->id)->get();
                        foreach ($row as $qwe) {
                            @unlink(public_path("uploads/" . $qwe->photo1));
                            @unlink(public_path("uploads/" . $qwe->photo2));
                            @unlink(public_path("uploads/" . $qwe->photo3));
                        }

                        $rows = DB::table('log_patrol_details')->where('id_log_patrols', $q)->delete();
                    }

                }
                Session::flash('delete', 'success');
                return redirect()->back();
            }
        }