0

I have muliple filter selecte query builder . I am not getting how to add whereIn with vaiable . In $resolution I am getting below array.

Array
(
    [0] => 4K
    [1] => HD
)

In $fps I am getting below array.

(
    [0] => 25/1
    [1] => 10/1
)

So i want fetch related data of 4K and HD values

My code as below

    function filter_each_video(Request $request){
            if(request()->ajax()){
                $where_str    = "1 = ?";
                // dd($req['resolution']);
    $where_params = array(1); 
    
                if($request->has('resolution') && ($request->get('resolution')!= null)){
                    $resolution = $request->get('resolution');
                   
                    $where_str .= "and format = '".$resolution."' ";
                    echo "<pre>";
                }
elseif($request->has('fps') && ($request->get('fps')!= null)){
                $fps = $request->get('fps');
                $where_str .= " fps = '$fps' ";
                
                // $item = Item::whereIn('fps', $fps)->get();
            }
               
              
               $item = Item::select('*')
      ->whereIn($where_str)
      ->orderByRaw(\DB::raw("FIELD(id, ".implode(",",$where_str).")"))
      ->get();
                
    
    
                $formats = DB::table('items')->select('format')->distinct()->get();
                $fps = DB::table('items')->select('fps')->distinct()->get();
                $data['item_count'] = $item;
                $data['item'] = $item;
               
                $html = view('frontend.pages.videos_for_each_filter', compact('data','formats','fps'))->render();

          
Praful
  • 171
  • 1
  • 16
  • what is the column name you want to filter using "whereIn"? – OMR Oct 16 '21 at 12:33
  • @OMR "format" is the column name in the database – Praful Oct 16 '21 at 12:37
  • Does this answer your question? [Can I do Model->where('id', ARRAY) multiple where conditions?](https://stackoverflow.com/questions/30706603/can-i-do-model-whereid-array-multiple-where-conditions) – mickmackusa May 04 '22 at 09:52

1 Answers1

0

The whereIn method verifies that a given column's value is contained within the given array.

the first argument is the column you want to make your 'where' on it, the second is the array that contains the options.

$optionsArray=['4K','HD']; // or you should get it from request or other source ...

and then:

 ->whereIn('format',$optionsArray)
OMR
  • 11,736
  • 5
  • 20
  • 35