Laravel 5.3“按计数分组”查询问题

时间:2016-10-19 09:48:57

标签: laravel laravel-5 laravel-5.2 laravel-5.1

请问我正在尝试在原始sql中运行这样的查询

SELECT `qnty`, COUNT(*) FROM cartlist GROUP BY `pro_id`,`cart_id`,`price`

在laravel。

我试过这个

$count = DB::table('cartlist')
                 ->select(DB::raw('count(*) as qnty'))
                 ->where('pro_id', '$tt->pro_id')
                 ->where('cart_id', '$cart_id')
                 ->groupBy('pro_id','price')
                 ->get();

但它会出现以下错误

类Illuminate \ Support \ Collection的对象无法转换为int

1 个答案:

答案 0 :(得分:1)

$count = DB::table('cartlist') ->select(DB::raw('count(*) as qnty')) 
                               ->where('pro_id', '$tt->pro_id') 
                               ->where('cart_id', '$cart_id') 
                               ->groupBy('pro_id','price','cart_id') ->get();