试图求和Laravel 5.5中的groupBy值

时间:2019-03-13 10:31:16

标签: laravel sum

我向我建议了此代码。我正在尝试总结stock_in_qty和stock_out_qty。

$warehouse1stocks = Warehouse1stocks::select(
                        "order_item_id",
                        Warehouse1stocks::raw('SUM(stock_in_qty) as stock_in_qty'),
                        Warehouse1stocks::raw('SUM(stock_out_qty) as stock_out_qty'))->groupBy('order_item_id')->get();
// dd($warehouse1stocks);
return view("warehouse1.index", compact("warehouse1stocks", $warehouse1stocks));

我的问题是这个错误

enter image description here

我试图从这里的其他问题中寻找答案,我认为我的代码似乎还可以,但是为什么我仍然遇到该错误?

您认为我的代码有什么问题?预先感谢!

2 个答案:

答案 0 :(得分:3)

代替写作:

UITableView

写:

Warehouse1stocks::raw('SUM(stock_in_qty) as stock_in_qty')

定义写表达式时

答案 1 :(得分:1)

使用这个

$warehouse1stocks = Warehouse1stocks::select(
                        "order_item_id",
                        \DB::raw('SUM(stock_in_qty) as stock_in_qty'),
                        \DB::raw('SUM(stock_out_qty) as stock_out_qty'))->groupBy('order_item_id')->get();
// dd($warehouse1stocks);
return view("warehouse1.index", compact("warehouse1stocks", $warehouse1stocks));