Hi, I think you should start by removing all the filters and start adding them one by one to monitor the results, this will make it easier to catch the bug. I would also refactor the code a lot and get rid of the @ because this is bad practice. Let's start here:
public function render_product_listing(Request $request, $filter_data = null)
{
$category_id = $request->input('category_id', '');
// Instead of:
// if(@!empty($data['category_id'])){
// $category_id = $data['category_id'];
// }else{
// $category_id = '';
// }
$sub_category_id = $request->input('sub_category_id', '');
$sub_sub_category_id = $request->input('sub_sub_category_id', '');
$brand_id = $request->input('brand_id', '');
$filter_id = $request->input('filter_id', '');
$products = Product::select('products.*','b.name as brand_name')
->with('product_image')
->whereHas('product_sizes.product_size')
->whereHas('product_color')
->join('brands as b','b.id','products.brand_id')
->where('status','A')
->whereNull('b.deleted_at')
->whereNull('products.deleted_at');
$brands = Brand::where('category_id',$category_id)
->whereNull('deleted_at');
if ($category_id) {
$category_id = $request->input('category_id');
$products->where('products.category_id', '=', $category_id);
$brands->where('category_id', '=', $category_id);
}
$products = $products->get()->toArray();
$sub_categories = SubCategory::with('sub_sub_categories')
->where('category_id',$category_id)
->whereNull('deleted_at')
->get()
->toArray();
$brands = $brands->get()->toArray();
if (Auth::check()) {
$user_id = Auth::id();
$wishlists = Wishlist::select()
->where('user_id',$user_id)
->pluck('product_id')
->toArray();
} else {
$user_id = null;
$wishlists = collect();
}
$response = view('frontEnd.products.common.product_listing', compact(
'products',
'sub_categories',
'brands',
'category_id',
'wishlists',
'sub_category_id',
'brand_id'
))->render();
$resp = [
'response' => $response,
'category_id' => $category_id,
'sub_category_id' => $sub_category_id,
'sub_sub_category_id' => $sub_sub_category_id,
'brand_id' => $brand_id,
'filter_id' => $filter_id
];
return $resp;
}