我的数据
invoice|amount|
0001 | 10000|
0001 | 10000|
现在我在Controller上的代码;
$invoices = Invoice::where('user_id', 1)
->where('status', 'Paid')
->orderBy('datePaid', 'desc')
->groupBy('no_invoice')
->get()
->sum('amount'); return view('invoice', ['invoices' => $invoices]);
我希望在我的foreach中使用get()和sum这样的字段数量显示所有数据。
invoice|total amount|
0001 | 20000|
您可以在select in query中使用selectRaw
,然后像这样使用sum(amount)
$invoices = Invoice::selectRaw('*, sum(amount) as total')
->where('user_id', 1)
->where('status', 'Paid')
->orderBy('datePaid', 'desc')
->groupBy('no_invoice')
->get();
现在你可以打印了
foreach($invoices as $invoice){
echo $invoice->total
}