diff --git a/beike/Models/Category.php b/beike/Models/Category.php index 9ff95262..be7a75a7 100644 --- a/beike/Models/Category.php +++ b/beike/Models/Category.php @@ -31,6 +31,11 @@ class Category extends Base return $this->hasMany(self::class, 'parent_id'); } + public function activeChildren(): HasMany + { + return $this->hasMany(self::class, 'parent_id')->where('active', true); + } + public function descriptions(): HasMany { return $this->hasMany(CategoryDescription::class); diff --git a/beike/Repositories/CategoryRepo.php b/beike/Repositories/CategoryRepo.php index 969e14d5..c7dd2085 100644 --- a/beike/Repositories/CategoryRepo.php +++ b/beike/Repositories/CategoryRepo.php @@ -90,7 +90,7 @@ class CategoryRepo { $topCategories = Category::query() ->from('categories as c') - ->with(['description', 'children.description']) + ->with(['description', 'activeChildren.description']) ->where('parent_id', 0) ->where('active', true) ->orderBy('position') diff --git a/beike/Shop/Http/Controllers/CategoryController.php b/beike/Shop/Http/Controllers/CategoryController.php index 763aa25c..65e2b275 100644 --- a/beike/Shop/Http/Controllers/CategoryController.php +++ b/beike/Shop/Http/Controllers/CategoryController.php @@ -17,6 +17,9 @@ class CategoryController extends Controller public function show(Request $request, Category $category) { + if (! $category->active) { + return redirect(shop_route('home.index')); + } $filterData = $request->only('attr', 'price', 'sort', 'order', 'per_page'); $products = ProductRepo::getProductsByCategory($category->id, $filterData); $category->load('description'); diff --git a/beike/Shop/Http/Resources/CategoryDetail.php b/beike/Shop/Http/Resources/CategoryDetail.php index 8e3901cf..3d98ddf6 100644 --- a/beike/Shop/Http/Resources/CategoryDetail.php +++ b/beike/Shop/Http/Resources/CategoryDetail.php @@ -21,8 +21,8 @@ class CategoryDetail extends JsonResource 'url' => shop_route('categories.show', ['category' => $this]), ]; - if ($this->relationLoaded('children') && $this->children->count() > 0) { - $item['children'] = self::collection($this->children); + if ($this->relationLoaded('activeChildren') && $this->activeChildren->count() > 0) { + $item['children'] = self::collection($this->activeChildren); } else { $item['children'] = []; }