Browse Source

分类改为把下级也显示出来

moshaorui 3 weeks ago
parent
commit
5d72f024c3
1 changed files with 13 additions and 1 deletions
  1. 13 1
      app/Http/Controllers/ProductController.php

+ 13 - 1
app/Http/Controllers/ProductController.php

@@ -42,8 +42,20 @@ class ProductController extends Controller
             ->where('dist_id', getDistId())
             ->firstOrFail();
 
+        if (!$category) {
+            abort('404');
+        }
+        $categoryIds = [$category->id];
+        if ($category) {
+            //找下一级分类
+            $subCategories = DistProductCategory::where('parent_id', $category->id)->get();
+            foreach ($subCategories as $subCategory) {
+                $categoryIds[] = $subCategory->id;
+            }
+        }
+
         // 获取分类下的所有产品,排序,然后进行分页
-        $products = DistProduct::where('category_id', $category->id)
+        $products = DistProduct::whereIn('category_id', $categoryIds)
             ->where('dist_id', getDistId())
             ->where('enabled', 1)
             ->where('status', 2)