Skip to content
Advertisement

how can I count same products in order table in laravel eloquent? I`m new Laravel user

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

Advertisement

Answer

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();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement