i have a problem with this query:
$winecount=App\Models\OrderProduct::where('id_order',$item->id_order)->where('id_wine',$item->id_wine)->groupBy('id_wine')->count();
i get:
| id_order | id_wine |
|---|---|
| 1 | 1 |
| 1 | 1 |
| 1 | 1 |
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
i want to get instead:
| id_wine | qty |
|---|---|
| 1 | 3 |
| 2 | 3 |
how can I modify the query so that I have the count for each "id_wine"?
for clarity this is my view:
Bottiglie ordinate:
Keane Nieves x10
Keane Nieves x10
Keane Nieves x10
Lucy Rasmussen x10
Lucy Rasmussen x10
Lucy Rasmussen x10
Nayda Duncan x10
Nayda Duncan x10
Nayda Duncan x10
Ruth Boyd x10
Ruth Boyd x10
Ruth Boyd x10
instead of:
Bottiglie ordinate:
Keane Nieves x30
Lucy Rasmussen x30
Nayda Duncan x30
Ruth Boyd x30
CodePudding user response:
You count the entire result. but you have to count the grouped by parts. That query would help you:
$user_info = DB::table('order_products')
->select('id_wine', DB::raw('count(*) as qty'))
->groupBy('id_wine')
->get();
sugested by Question Owner
$winecount = OrderProduct::selectRaw('count(*) as qty')
->groupBy('id_wine')
->get();
