$year = date("y");
for($i=1;$i<=12;$i++)
{
$MonthlyReceive = DB::table('order_items') ->whereBetween('created_at',array($year.'-'.$i.'-1',$year.'-'.$i.'-31')) ->select(DB::raw('sum(price*quantity)'))->where('quantity','<','0');
return $MonthlyReceive;
}
// table name "order_items"
// id |product_id |quantity |price |order_id
答案 0 :(得分:2)
您可以每月获取明智的汇总数据,而无需循环。
group by
只需将{{1}}与聚合功能一起使用。
答案 1 :(得分:0)
$year = date("y");
for($i=1; $i<=12; $i++) {
$MonthlyReceive = DB::table('order_items')
->whereBetween('created_at', array($year.'-'.$i.'-1',$year.'-'.$i.'-31'))
->where('quantity', '>', '0')
->sum(DB::raw('price*quantity'));
return $MonthlyReceive;
}
我已使用此代码修复了该问题。