在产品价格总和列旁边显示每周交易

时间:2019-10-11 09:34:49

标签: php mysql laravel eloquent

我的控制器中有此代码,该代码显示产品总价(每周)


$weekly = Transaction::where('user_id',$id)->orderBy('created_at','desc')->get()->groupBy(function($date) {
             return Carbon::parse($date->created_at)->format('W');
             })->map(function ($row) {
                 return $row->sum('product_price');
             });

我也有显示每周交易的信息


$weekly = Transaction::where('user_id',$id)->orderBy('created_at','desc')->get()->groupBy(function($date) {
             return Carbon::parse($date->created_at)->format('W');
             });

我要显示每周交易以及产品总价(每周)。请问我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试

 $res = Transaction::select(DB::raw('SUM(product_price) AS total'))
        ->orderBy('created_at','desc')
        ->groupBy(Carbon::parse(created_at)->format('W'))
        ->where('user_id',$id)
        ->get();