From 43c7a5461ec567f24cc7efdf5b220b614d6fe982 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Mon, 27 Feb 2023 09:53:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A6=81=E7=94=A8=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E9=97=AE=E9=A2=98=20https://guangdagit.com/projects/b?= =?UTF-8?q?k=5Fmundosanjose/issues/8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Models/Category.php | 5 +++++ beike/Repositories/CategoryRepo.php | 2 +- beike/Shop/Http/Controllers/CategoryController.php | 3 +++ beike/Shop/Http/Resources/CategoryDetail.php | 4 ++-- 4 files changed, 11 insertions(+), 3 deletions(-) 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'] = []; }