我想按货币合计金额
我这样在SQL中做
select sum(total),currencies.currency_symbol from invoices
join(currencies)
where(invoices.currency_id = currencies.id)
group by(currency_id)
我在Laravel这样尝试过
$sum = Invoice::sum('total')
->select('currency_symbol')
->join('currencies','invoices.currency_id','=','currencies.id')
->groupBy('currency_id');
return response()->json($sum);
但是会引发错误
Symfony \ Component \ Debug \ Exception \ FatalThrowableError:调用字符串上的成员函数
select()
请给我解决办法
答案 0 :(得分:0)
我认为您必须使用selectRaw,因为sum和select的组合我认为不会起作用,因此可以使用两者之一。您可以尝试这样的事情:
$sum = Invoice::selectRaw('currency_symbol, sum(total) as sum')
->join('currencies','invoices.currency_id','=','currencies.id')
->groupBy('currency_id');
return response()->json($sum);