diff --git a/app/Helpers/function.php b/app/Helpers/function.php index 0d1e296..35a8b6e 100644 --- a/app/Helpers/function.php +++ b/app/Helpers/function.php @@ -19,6 +19,27 @@ function getOneCategory($categoryId, $return = '') return new Category; } +/** + * Notes: 获取所有下级 + * + * @Author: 玄尘 + * @Date: 2022/8/19 17:24 + * @param $array + * @param $categoryId + * @return array + */ +function getAllChild($categories, $categoryId) +{ + $arr = array(); + foreach ($categories as $category) { + if ($category['parent_id'] == $categoryId) { + $arr[] = $category['id']; + $arr = array_merge($arr, getAllChild($categories, $category['id'])); + }; + }; + return $arr; +} + /** * Notes: 获取文章分类详情 * diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index c12a84d..10c63ac 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -45,6 +45,14 @@ class CategoryController extends Controller ->latest('created_at') ->paginate(); + } elseif ($category->id == 7) { + $categories = Category::get(); + $children = array_merge([$category->id], getAllChild($categories, $category->id)); + $articles = Article::ByCategory($children) + ->where('status', 1) + ->latest('sort') + ->latest('created_at') + ->paginate(); } else { $articles = $category->relations($category->type) ->where('status', 1)