dream8877 2018-05-15 11:44
浏览 79
已采纳

如何在雄辩的orm中结合sum()和group by语句?

I want to produce record who retrieve data sales in specified date range with 2 parameter input $date_from and $date_to so I am trying to combine some SQL function to do that:

public function print_report(Request $request) {
    $from = "2018-04-03";
    $to = "2018-04-17";
    $data = AppSalesDetail::select('app_sales_detail.*','app_sales.*')
        ->leftJoin('app_sales','app_sales.app_sales_id','=','app_sales_detail.app_sales_id')    
        ->whereBetween('sale_date', array($from, $to))
        ->groupBy("app_sales_detail.app_sales_id")  
        ->sum('app_sales_detail.sub_total')
        ->get();
}

Before I add ->sum('app_sales_detail.sub_total') inside the query everything works fine as my expect but happen this error after I insert that function

Call to a member function groupBy() on string

please give me advice to solve that.

  • 写回答

1条回答 默认 最新

  • dtol41388 2018-05-15 14:54
    关注

    The sum() method only returns one row, you have to add it to select().
    You also can't select all the columns:

    $data = AppSalesDetail::select('app_sales_detail.app_sales_id')
        ->selectRaw('SUM(sub_total) sub_total')
        ->leftJoin('app_sales','app_sales.app_sales_id','=','app_sales_detail.app_sales_id')    
        ->whereBetween('sale_date', array($from, $to))
        ->groupBy("app_sales_detail.app_sales_id")  
        ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器