Bläddra i källkod

fix: 分类或子分类存在产品不能删除

xiansin 2 år sedan
förälder
incheckning
bf67b1f87c
1 ändrade filer med 17 tillägg och 0 borttagningar
  1. 17 0
      server/app/Admin/Controllers/ProductCategoryController.php

+ 17 - 0
server/app/Admin/Controllers/ProductCategoryController.php

xqd xqd
@@ -2,6 +2,7 @@
 
 namespace App\Admin\Controllers;
 
+use App\Models\Product;
 use App\Models\ProductCategory;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
@@ -10,6 +11,22 @@ use Dcat\Admin\Http\Controllers\AdminController;
 
 class ProductCategoryController extends AdminController
 {
+
+    public function destroy($id)
+    {
+        $childIds = ProductCategory::where('pid',$id)->get()->pluck('id');
+        $childIds[]= $id;
+
+        $hasEpisode = Product::whereIn('cate_id',$childIds)->count();
+        if($hasEpisode){
+            $form = new Form();
+            $response = $form->response();
+            return $response->error('当前分类或子分类存在产品,前先删除产品后再删除分类');
+        }
+
+        return $this->form()->destroy($id);
+    }
+
     /**
      * Make a grid builder.
      *