很难将SQL查询转换为Laravel Builder



我正在尝试使用查询构建器将以下查询转换为laravels,但与croups by Claile by Carrause和总和汇总函数

有一些问题
SELECT code, SUM(quantity) as quantity, SUM(sale_price) as price
FROM `orders` o
GROUP By code

我已经尝试过,这是我绝望的尝试之后所拥有的。

$args['orders'] = DB::table('orders')
->havingRaw('sum(sale_price)')
->havingRaw('code')
->where([ 'shop_id' => $shop_id ])
->groupBy('code')
->get();

尝试

$args['orders'] = DB::table('orders')
->where('shop_id', '=', $shop_id)
->select('code', DB::raw('sum(quantity) as quantity, sum(sale_price) as price'))
->groupBy('code')
->get();

订购
$args['orders'] = DB::table('orders')
->where('shop_id', '=', $shop_id)
->select('code', DB::raw('sum(quantity) as quantity, sum(sale_price) as price'))
->groupBy('code')
->orderBy('quantity')
->get();

或尝试

->orderByRaw('sum(quantity)')

最新更新