diff --git a/app/Http/Controllers/Api/V1/Filters/FilterController.php b/app/Http/Controllers/Api/V1/Filters/FilterController.php index d40a442..d326d85 100644 --- a/app/Http/Controllers/Api/V1/Filters/FilterController.php +++ b/app/Http/Controllers/Api/V1/Filters/FilterController.php @@ -62,29 +62,29 @@ class FilterController extends Controller private function brands() { if ($this->shouldFilterByBrand()) { - return Brand::query()->where('id', $this->request->brand_id)->get(['id', 'name']); + return Brand::where('id', $this->request->brand_id)->get(['id', 'name']); } if ($this->shouldFilterByCategory()) { - $brands = Category::query()->find($this->request->category_id)->products() + $brands = Category::find($this->request->category_id)->products() ->where('products.is_visible', true) ->where('products.parent_id', null) ->where('products.stock', '>', 0) ->distinct('products.brand_id') ->pluck('products.brand_id'); - return Brand::query()->whereIntegerInRaw('id', $brands)->get(['id', 'name']); + return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']); } if ($this->shouldFilterByCollection()) { - $brands = Collection::query()->find($this->request->collection_id)->products() + $brands = Collection::find($this->request->collection_id)->products() ->where('products.is_visible', true) ->where('products.parent_id', null) ->where('products.stock', '>', 0) ->distinct('products.brand_id') ->pluck('products.brand_id'); - return Brand::query()->whereIntegerInRaw('id', $brands)->get(['id', 'name']); + return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']); } return Brand::query()->where('is_visible', true)->ordered()->get(['id', 'name']); @@ -102,18 +102,13 @@ class FilterController extends Controller ->distinct('products.id') ->pluck('products.id'); - return Category::query() - ->distinct('categories.id') - ->where('is_visible', true) - ->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id') - ->where('product_has_relations.productable_type', '=', 'category') - ->whereIntegerInRaw('product_has_relations.product_id', $products) - ->get() - ->map(fn ($category) => [ - 'id' => $category->id, - 'parent_id' => $category->parent_id, - 'name' => $category->name, - ]); + return Category::where('is_visible', true) + ->ordered() + ->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id') + ->where('product_has_relations.productable_type', '=', 'category') + ->whereIntegerInRaw('product_has_relations.product_id', $products) + ->get(['categories.id', 'categories.parent_id', 'categories.name']) + ->unique('categories.id'); } /**