【发布时间】:2016-10-19 09:48:57
【问题描述】:
请我尝试在原始 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
【问题讨论】:
-
为什么
$cart_id和$tt->pro_id周围有单引号? -
$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();
-
但仍然出现该错误
-
您的原始 sql 查询和 laravel 查询确实不同。根据您的原始 SQL 查询,您按
pro_id、cart_id、price分组,根据您的 laravel 查询,您仅按“pro_id”分组,并使用其他三个作为 where 条件。你想达到什么目的?
标签: laravel laravel-5 laravel-5.2 laravel-5.1