filled('query')) { $model = $model->search(request('query')); $productIds = $model->keys(); } $query = $model->filter($productFilter); if (request()->filled('category')) { $productIds = (clone $query)->select('products.id')->resetOrders()->pluck('id'); } $products = $query->paginate(request('perPage', 30)); event(new ShowingProductList($products)); return response()->json([ 'products' => $products, 'attributes' => $this->getAttributes($productIds), ]); } private function getAttributes($productIds) { if (!request()->filled('category') || $this->filteringViaRootCategory()) { return collect(); } return Attribute::with('values') ->where('is_filterable', true) ->whereHas('categories', function ($query) use ($productIds) { $query->whereIn('id', $this->getProductsCategoryIds($productIds)); }) ->get(); } private function filteringViaRootCategory() { return Category::where('slug', request('category')) ->firstOrNew([]) ->isRoot(); } private function getProductsCategoryIds($productIds) { return DB::table('product_categories') ->whereIn('product_id', $productIds) ->distinct() ->pluck('category_id'); } }