我有一个“订单”表,其中包含我店中每个订单的每行总计。我需要计算每一行的所有总数才能获得总计。我如何在Laravel中设置我的查询?
我的订单表:
我的功能:
public function index() {
// Get all the orders in DB
$orders = Order::all();
// THAT DOESNT WORK, it just counts the rows
$count_total = DB::table('orders')->select('count(total)')->count();
dd($count_total);
// Get all the carts in DB
$carts = Cart::all();
return view('admin.pages.index', compact('orders', 'carts', 'count_products'));
}
答案 0 :(得分:4)
您可以使用Eloquent方式,通过这样做,您可以获得列的总和。
$total = Orders::sum('total');
echo "<pre>";
print_r($total);
echo "<pre>";
答案 1 :(得分:2)
尝试sum()
aggrigate方法:
$count_total = DB::table('orders')->sum('total');