【问题标题】:how to delete multiple photos from the folder when deleting it from database?从数据库中删除时如何从文件夹中删除多张照片?
【发布时间】:2020-03-16 17:33:12
【问题描述】:

当我选中多个复选框以删除多行时,它会删除带有图像 url 表单数据库的记录,但不会从公用文件夹中删除图像。如果有人可以帮助我。

对于删除单个记录,以下代码完美运行,它删除了记录表单数据库,也从文件夹中删除了照片。

public function destroy($id)
    {
        //

        // $this->authorize('isAdmin');
        $Employee = Employee::findOrFail($id);
         $currentPhoto = $Employee->photo;
         $currentdatePhoto = $Employee->afghanidatephoto;
        $EmployeePhoto = (public_path('img/emp/').$currentPhoto);
         $EmployeedatePhoto = (public_path('img/date/').$currentdatePhoto);
        if(file_exists($EmployeedatePhoto)){
            @unlink($EmployeedatePhoto);

        }
       if(file_exists($EmployeePhoto))
        {
             @unlink($EmployeePhoto);

        }
        $Employee->delete();
        return ['message'=>'Employee Deleted Successfully'];
    }

EmployeeController 中的函数:

public function multipledelete(Request $request)
            {
                try 
                    {  
                    Employee::whereIn('id', $request->id)->delete(); 

                        return response()->json('data deleted');
                    }

                    catch (Exception $e) {
                        return response()->json($e->getMessage(), 500);
                    }
             }

API 中的代码:

Route::delete('multipledelete','API\EmployeeController@multipledelete');

Employee.vue 中用于删除操作的代码是:

delt() {
      var chekboxs = document.getElementById("chekboxs");
      if (chekboxs.checked) {
        swal
          .fire({
            title: "Are you sure you want to delete the selected records?",
            text: "You won't be able to revert this!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
          })
          .then(result => {
            //Send request to the server
            if (result.value) {
              axios
                .delete("api/multipledelete", {
                  params: { id: this.checkedRows }
                })
                .then(() => {
                  toast.fire({
                    type: "success",
                    title: "Your Selected Employees are successfully deleted!"
                  });
                  Fire.$emit("refreshPage");
                })
                .catch(e => {
                  console.log(e);
                });
            }
          });
      } else {
        toast.fire({
          type: "warning",
          title: "You didn't check anything to be deleted please check it!"
        });
      }
    }

复选框是:

<div class="custom-control custom-checkbox">
                      <input
                        class="form-check-input"
                        type="checkbox"
                        :value="employee.id"
                        v-model="checkedRows"
                        id="chekboxs"
                      />
                      <label class="form-check-label"></label>
                    </div>

按钮是:

<div class="col-md-2" style="margin-bottom:-29px;">
                    <button class="btn btn-danger" @click="delt">
                      <i class="fas fa-user-minus"></i>
                      Delete Multiple
                      <!-- <grid-loader v-show="seen" :loading="loading" :color="color" :size="size"></grid-loader> -->
                    </button>
                  </div>

我试过这段代码,但 id 不起作用。

public function multipledelete(Request $request)
            {
                try 
                    {  
                    $Employee = Employee::whereIn('id', $request->id)->delete(); 
                    $currentPhoto = $Employee->photo;
         $currentdatePhoto = $Employee->afghanidatephoto;
        $EmployeePhoto = (public_path('img/emp/').$currentPhoto);
         $EmployeedatePhoto = (public_path('img/date/').$currentdatePhoto);
        if(file_exists($EmployeedatePhoto)){
            @unlink($EmployeedatePhoto);

        }
       if(file_exists($EmployeePhoto))
        {
             @unlink($EmployeePhoto);

        }
        $Employee->delete();
                        return response()->json('data deleted');
                    }

                    catch (Exception $e) {
                        return response()->json($e->getMessage(), 500);
                    }
             }

【问题讨论】:

    标签: javascript mysql laravel vue.js


    【解决方案1】:

    考虑到您当前的代码,最简单的方法是简单地遍历员工并根据需要删除:

    $employees = Employee::whereIn('id', $request->id)->get(); 
    foreach($employees AS $employee){
        $currentPhoto = $employee->photo;
        $currentdatePhoto = $employee->afghanidatephoto;
    
        $employeePhoto = (public_path('img/emp/').$currentPhoto);
        $employeedatePhoto = (public_path('img/date/').$currentdatePhoto);
    
        if(file_exists($employeedatePhoto)){
            @unlink($employeedatePhoto);
        }
    
        if(file_exists($employeePhoto)){
            @unlink($employeePhoto);
        }
    
        $employee->delete();
    }
    

    您可以将foreach() 包装在try { ... } catch { ... } 块中。

    您的代码出现问题的原因从这一行开始:

    $Employee = Employee::whereIn('id', $request->id)->delete(); 
    

    通过使用-&gt;delete() 完成该查询,您将删除与ID 匹配的记录。接下来,尝试对$Employee 执行操作将不起作用,因为-&gt;delete() 不会返回object(我相信它会返回一个布尔值,true),所以$Employee-&gt;{ ... } 是一个错误。

    考虑到所有这些,您应该能够查询、循环和删除您的员工及其图像。

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      相关资源
      最近更新 更多