Here is my Orders table. How many same product are sell.
order_id product_id price
1 2 100
2 3 50
3 2 100
4 3 50
5 1 150
6 2 100
I needed
List product_id total_count
1 2 3
2 3 2
3 1 1
try something like this
$product_count= DB::table('orders')->select('product_id', DB::raw('count(product_id) as product_id'))->groupBy('product_id')->get();
Group By product ID and take the count of products in order
You can write your query n LARAVEL Eloquent in this way.
$response = \DB::table('orders')
->select('product_id as List', \DB::raw('COUNT(product_id) as total_count'), 'product_id')
->groupBy('product_id')
->get();
I have solved it, you can use the below code, it is written in Laravel Eloquent as you asked:
$products = Orders::select('product_id', DB::raw('count(product_id) AS total_count'))->groupBy('product_id')->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.