From 97f3db810a57705c6203ef95204a65f7c8849845 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Wed, 9 Nov 2022 17:53:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E5=90=8E=E5=8F=B0=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Http/Controllers/CategoryController.php | 5 +-- beike/Models/Category.php | 6 +++ beike/Repositories/CategoryRepo.php | 39 +++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/beike/Admin/Http/Controllers/CategoryController.php b/beike/Admin/Http/Controllers/CategoryController.php index e67ca1d8..4fa69fcc 100644 --- a/beike/Admin/Http/Controllers/CategoryController.php +++ b/beike/Admin/Http/Controllers/CategoryController.php @@ -15,10 +15,7 @@ class CategoryController extends Controller public function index() { - $categories = Category::with('description', 'children.description', 'children.children.description') - ->where('parent_id', 0) - ->get(); - + $categories = CategoryRepo::getAdminList(); $data = [ 'categories' => CategoryResource::collection($categories), ]; diff --git a/beike/Models/Category.php b/beike/Models/Category.php index 6175956b..6ef8f355 100644 --- a/beike/Models/Category.php +++ b/beike/Models/Category.php @@ -2,6 +2,7 @@ namespace Beike\Models; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -20,6 +21,11 @@ class Category extends Base 'active' => 'boolean', ]; + public function parent(): BelongsTo + { + return $this->belongsTo(Category::class, 'parent_id'); + } + public function children(): HasMany { return $this->hasMany(Category::class, 'parent_id'); diff --git a/beike/Repositories/CategoryRepo.php b/beike/Repositories/CategoryRepo.php index 80022c04..c54e7a6d 100644 --- a/beike/Repositories/CategoryRepo.php +++ b/beike/Repositories/CategoryRepo.php @@ -22,6 +22,41 @@ class CategoryRepo private static $allCategoryWithName = null; + /** + * 后台获取分类列表 + * @return Builder[]|Collection + */ + public static function getAdminList() + { + self::cleanCategories(); + return Category::with(['description', 'children.description', 'children.children.description']) + ->where('parent_id', 0) + ->get(); + } + + + /** + * 清理分类数据 + */ + public static function cleanCategories() + { + $categories = Category::with([ + 'parent', + 'description', + ])->get(); + + foreach ($categories as $category) { + if ($category->parent_id && empty($category->parent)) { + $category->parent_id = 0; + $category->save(); + } + if (empty($category->description)) { + $category->delete(); + } + } + } + + public static function flatten(string $locale, $separator = ' > '): array { $sql = "SELECT cp.category_id AS id, TRIM(LOWER(GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '{$separator}'))) AS name, c1.parent_id, c1.position"; @@ -108,6 +143,10 @@ class CategoryRepo if ($pathName) { $pathName .= ' > '; } + if (empty($path->pathCategory)) { + $path->delete(); + continue; + } $pathName .= $path->pathCategory->description->name; } $results[] = [