From e522a263fea0f9c522aaa3c1c7f19392e6ea0350 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Tue, 28 Jun 2022 19:49:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E7=B1=BB=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Models/Category.php | 12 +++++++----- beike/Repositories/CategoryRepo.php | 1 + beike/Shop/Http/Resources/CategoryList.php | 2 ++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/beike/Models/Category.php b/beike/Models/Category.php index d93d73e8..6ead15c7 100644 --- a/beike/Models/Category.php +++ b/beike/Models/Category.php @@ -2,8 +2,10 @@ namespace Beike\Models; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Category extends Model { @@ -19,22 +21,22 @@ class Category extends Model 'active' => 'boolean', ]; - public function children() + public function children(): HasMany { return $this->hasMany(Category::class, 'parent_id'); } - public function descriptions() + public function descriptions(): HasMany { return $this->hasMany(CategoryDescription::class); } - public function description() + public function description(): HasOne { return $this->hasOne(CategoryDescription::class)->where('locale', locale()); } - public function paths() + public function paths(): HasMany { return $this->hasMany(CategoryPath::class); } diff --git a/beike/Repositories/CategoryRepo.php b/beike/Repositories/CategoryRepo.php index 84225c25..173bde24 100644 --- a/beike/Repositories/CategoryRepo.php +++ b/beike/Repositories/CategoryRepo.php @@ -27,6 +27,7 @@ class CategoryRepo ->from('categories as c') ->with(['description', 'children.description']) ->where('parent_id', 0) + ->orderBy('position') ->get(); $categoryList = CategoryList::collection($topCategories); diff --git a/beike/Shop/Http/Resources/CategoryList.php b/beike/Shop/Http/Resources/CategoryList.php index 8c9ce8a5..e00ea0bf 100644 --- a/beike/Shop/Http/Resources/CategoryList.php +++ b/beike/Shop/Http/Resources/CategoryList.php @@ -23,6 +23,8 @@ class CategoryList extends JsonResource if ($this->relationLoaded('children') && $this->children->count() > 0) { $item['children'] = self::collection($this->children); + } else { + $item['children'] = []; } return $item; }