This commit is contained in:
Edward Yang 2023-02-27 09:53:00 +08:00
parent e1430873a1
commit 43c7a5461e
4 changed files with 11 additions and 3 deletions

View File

@ -31,6 +31,11 @@ class Category extends Base
return $this->hasMany(self::class, 'parent_id');
}
public function activeChildren(): HasMany
{
return $this->hasMany(self::class, 'parent_id')->where('active', true);
}
public function descriptions(): HasMany
{
return $this->hasMany(CategoryDescription::class);

View File

@ -90,7 +90,7 @@ class CategoryRepo
{
$topCategories = Category::query()
->from('categories as c')
->with(['description', 'children.description'])
->with(['description', 'activeChildren.description'])
->where('parent_id', 0)
->where('active', true)
->orderBy('position')

View File

@ -17,6 +17,9 @@ class CategoryController extends Controller
public function show(Request $request, Category $category)
{
if (! $category->active) {
return redirect(shop_route('home.index'));
}
$filterData = $request->only('attr', 'price', 'sort', 'order', 'per_page');
$products = ProductRepo::getProductsByCategory($category->id, $filterData);
$category->load('description');

View File

@ -21,8 +21,8 @@ class CategoryDetail extends JsonResource
'url' => shop_route('categories.show', ['category' => $this]),
];
if ($this->relationLoaded('children') && $this->children->count() > 0) {
$item['children'] = self::collection($this->children);
if ($this->relationLoaded('activeChildren') && $this->activeChildren->count() > 0) {
$item['children'] = self::collection($this->activeChildren);
} else {
$item['children'] = [];
}