diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index ef629d4..e62b90e 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -36,7 +36,8 @@ class CategoryController extends Controller ->paginate(); $parent = $category; - if ($category->childrens->isEmpty() && $category->parent) { + if (! $category->getChildrenCount() && $category->parent) { + $parent = $category->parent; } diff --git a/app/Models/Category.php b/app/Models/Category.php index b3e3f97..b0d3715 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -93,4 +93,16 @@ class Category extends Model return $parent; } + /** + * Notes: 获取下级分类数量 + * + * @Author: 玄尘 + * @Date: 2021/12/10 15:27 + * @return int + */ + public function getChildrenCount(): int + { + return $this->childrens()->where('status', 1)->count(); + } + }