Laravel advanced search query fix

后端 未结 8 1324
终归单人心
终归单人心 2021-01-28 21:34

I have a search form with multiple input and select boxes I need help to get if conditions in my query in order to each part works separately and all at once.

he

8条回答
  •  不知归路
    2021-01-28 21:51

    I suggest tu use each separeted and its help you to feature easaly manupulate code

    as your typical condition your sub_option come from third table last relation ship is used.

     if(count($request['suboptions'])) {
    
             $product->whereHas('options',function($options) use ($request) {
    
                       $options->whereHas('suboptions',function($suboption)use($request) {
    
                             $suboption->whereIn('id',$request['suboptions']);
                      });
             }); 
     }
    

    for min price max price i assume your price in procuct table

       if(! empty($request['min_price'])) {
    
              $product->where('price','>=',$request['min_price']);
        }
    
     if(! empty($request['max_price'])) {
    
              $product->where('price','<=',$request['max_price']);
        }
    

    for brand as you say brand_id column in product table then

       if(count($request['brands'])) {
    
              $product->whereIn('brand_id',$request['brands']);
        } 
    

提交回复
热议问题