首页 > 解决方案 > 如何在laravel中删除涉及图像的多条记录

问题描述

请大家知道如何删除涉及图像的多条记录。我不知道我可以使用任何方法。我已经尝试了很多。这是我在下面尝试过的。请帮助我,我真的需要你的帮助。提前致谢

请这是下面的代码

public function multipleUserDelete(Request $request,$id, $post_image){

    if ($request->isMethod("post")) {
        $data=$request->all();   

        //$del_user = $request->del_user;
        // $ids=$del_user[];
        //foreach(session('posts') as $session){


        //foreach(session('products') as $postDelete){

        $postDeletes=Post::where(['id'=> $id])
                            ->where('post_image', $post_image)
                            ->get();

        foreach ($postDeletes as $postDelete) {
            # code...

            // $postDeletes=Post::where(['id'=> $id])->get();
    //}
            $large_image_paths='images/backend_image/admin_users/small/';

            $medium_image_paths='images/backend_image/admin_users/medium/';

            $small_image_paths='images/backend_image/admin_users/large/';

            //Delete Image permenently from product table begins 

            //Delete Large image if not exist

            if(file_exists($large_image_paths. $postDelete->post_image)){
                unlink($large_image_paths. $postDelete->post_image);    
            }

            //Delete Large image if not exist            
            if(file_exists($small_image_paths. $postDelete->post_image)){
                unlink($small_image_paths. $postDelete->post_image);    
            }

            //Delete Medium image if not exist
            if(file_exists($medium_image_paths. $postDelete->post_image)){
                unlink($medium_image_paths. $postDelete->post_image);    
            }
        }


        //$del_id=$request->input('del_feedback');

        Post::whereIn('id', $data['del_user'])->delete();

        return redirect()->back()->with("flash_message_success","you Successfully Deleted The Selected Users(s)");
    } 

标签: phplaraveleloquentlaravel-6

解决方案


public function multipleUserDelete(Request $request,$id, $post_image){

    if ($request->isMethod("post")) {
        $data=$request->all();   

        $postDeletes=Post::where(['id'=> $id])
                            ->where('post_image', $post_image)
                            ->get();
        $img_array = array();
        foreach ($postDeletes as $postDelete) {
            $large_image_paths='images/backend_image/admin_users/small/';

            $medium_image_paths='images/backend_image/admin_users/medium/';

            $small_image_paths='images/backend_image/admin_users/large/';
            $img='';
            if(file_exists(public_path($large_image_paths. $postDelete->post_image))){
                $img = $large_image_paths. $postDelete->post_image;
            }

            if(file_exists(public_path($small_image_paths. $postDelete->post_image))){
                $img = $small_image_paths. $postDelete->post_image;
            }

            if(file_exists(public_path($medium_image_paths. $postDelete->post_image))){
                $img = $medium_image_paths. $postDelete->post_image;
            }

            array_push($img_array,$img);
        }

        \File::delete($img_array);
        Post::whereIn('id', $data['del_user'])->delete();

        return redirect()->back()->with("flash_message_success","you Successfully Deleted The Selected Users(s)");
    } 

推荐阅读