How to execute two where conditions in laravel where order_id is same but product_ids are in array

xXx_HAWK :

So I have a table ordered_products ordered_products table structure

I have order id and multiple products_id now I want to fetch result based on orders_id and products_id for example

orders_id =10 , products_ids = array(15,20,21,23)

$result = array();

 foreach (products_ids as $value) {
  $result=DB::table('ordered_products')->where('orders__id','=',10)->where('products_id','=',$value)->get();
  }

I executed the query, but I am only getting single row. Please help me execute the query.

Rakesh Jakhar :

You should use where for order_id and whereIn for product_ids

$result = DB::table('ordered_products')
            ->where('orders__id',10)
            ->whereIn('products_id', $products_ids)
            ->get();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=346903&siteId=1