How can i count product id row in order details table by using order id in laravel

55 Views Asked by At

I can placed here demo table one is order table another is order_details table

  1. in order table
id order_total status
1 300 Delivered
2 500 Delivered
  1. and another table order_details
id order_id product_id product_price quantity
1 1 1 150 1
2 1 2 150 1
3 2 2 300 2
4 2 3 200 1

i want this type data

sl order_id count_unique_product_id_row
1 1 2
2 2 2
1

There are 1 best solutions below

0
Bilal Saad On BEST ANSWER

To resolve this issue do this code:

// get all rows in Order table

$orders = Order::all();

// Loop Through all orders rows

foreach($orders as $order) {

 $orderProductCount = OrderDetails::where("order_id", $order->id)->count();

 echo "Order ID" . $order->id . " Product Count". $orderProductCount;

}