From 486db5a1abece0ff2ebbd3fbae1e79bf2288d5be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E5=B0=98?= <122383162@qq.com> Date: Fri, 19 Aug 2022 20:00:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Helpers/function.php | 21 +++++++++++++++++++++ app/Http/Controllers/CategoryController.php | 8 ++++++++ 2 files changed, 29 insertions(+) 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)