tani's avatar
Level 1

I need help for search page in laravel

I want to show products according to categories and sub categories selected. Right now no products are showing up while selecting the categories and sub categories even though the products are their in the database. Below is the code I am using:-

public function render_product_listing(Request $request,$filter_data=null){
    
    $data = $request->all();
    
    if(@!empty($data['category_id'])){
        $category_id = $data['category_id'];
    }else{
        $category_id = '';
    }
    if(@!empty($data['sub_category_id'])){
        $sub_category_id = $data['sub_category_id'];
    }else{
        $sub_category_id = '';
    }
    if(@!empty($data['sub_sub_category_id'])){
        $sub_sub_category_id = $data['sub_sub_category_id'];
    }else{
        $sub_sub_category_id = '';
    }
    if(@!empty($data['brand_id'])){
        $brand_id = $data['brand_id'];
    }else{
        $brand_id = '';
    }
    if(@!empty($data['filter_id'])){
        $filter_id = $data['filter_id'];
    }else{
        $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');

    // $sub_category_id = '';
    // $sub_sub_category_id = '';
    // $brand_id = '';
    if(!empty($filter_data)){
        $filter_data = explode('_', $filter_data);
        $category = $filter_data[0];
        if($category=='cat'){
            $category_id = $filter_data[1];
        }
        if($category=='sub'){
            $sub_category_id = $filter_data[1];
        }
        if($category=='subsub'){
            $sub_sub_category_id = $filter_data[1];
    // dd($sub_sub_category_id);
        }
        if($category=='brand'){
            $brand_id = $filter_data[1];
        }
        if($category=='filter'){
            $filter_id = $filter_data[1];
        }
        if($category=='low'){
            $low = $filter_data[1];
            // $products    = $products->orderBy('products.final_price','asc');
        }
        if($category=='high'){
            $high = $filter_data[1];
        }
        if($category=='new'){
            $new = $filter_data[1];
        }
        if($category=='featured'){
            $featured = $filter_data[1];
        }
    }
    if(!empty($sub_sub_category_id)){
        $products    = $products->where('products.sub_sub_category_id',$sub_sub_category_id);
        $brands = $brands->where('sub_sub_category_id',$sub_sub_category_id);
    }
    if(!empty(@$sub_category_id)){
     
        $products    = $products->where('products.sub_category_id',$sub_category_id);
        $category_id = SubCategory::where('id',$sub_category_id)
                                   ->whereNull('deleted_at')
                                   ->value('category_id');
        $brands = $brands->where('sub_category_id',$sub_category_id);
    }
    if(!empty(@$category_id)){
     
        $products    = $products->where('products.category_id',$category_id);
       
        $brands = $brands->where('category_id',$category_id);
    }
 
    if(!empty(@$brand_id)){
       
        $products    = $products->where('brand_id',$brand_id);
        $category_id = Brand::where('id',$brand_id)
                            ->whereNull('deleted_at')
                            ->value('category_id');
        $brands = $brands->where('id',$brand_id);
    }

    if(!empty(@$filter_id)){
                    
        $from_value = Filter::where('id',$filter_id)->value('from_value');
                  
        $products   = $products->where('products.discount_percent','>=',$from_value);

    }
    
    if (!empty(@$low)) {
        $products    = $products->orderBy('products.final_price','asc');
    }
    if (!empty(@$high)) {
        $products    = $products->orderBy('products.final_price','desc');
    }
    if (!empty(@$new)) {
        $products    = $products->orderBy('products.id','desc');
    }
    if (!empty(@$featured)) {
        $products    = $products->whereHas('featured_products');
    }

    $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();
    }
    $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;
}

}

0 likes
4 replies
CliffordAtCaveoDotNL's avatar

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;
}
tani's avatar
Level 1

Thanks for the help :) But still the products aren't showing up.

Request URL: https://stename/get/filtered/product/sub_16 Request Method: GET Status Code: 500 Internal Server Error Remote Address: 54.146.204.200:443 Referrer Policy: no-referrer-when-downgrade

This is what I am getting in console.

CliffordAtCaveoDotNL's avatar

Go all the way back to the beginning:

Does this work:

public function render_product_listing(Request $request, $filter_data = null)
{
    return [];
}

If so, does this work:

public function render_product_listing(Request $request, $filter_data = null)
{
    $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')
        ->get();

    return [
        'products' => $products->toArray(),
    ];
}

And so on, try to identify where the problem is simply adding more code, once it breaks you have found your problem.

1 like
tani's avatar
Level 1

Now it is not showing error in console, but still products are not coming up. You are right I need to identify the problem by adding more code. Thanks :)

Please or to participate in this conversation.