2016-04-22 63 views
2

我有一個「訂單」表,它包含我店中每個訂單的每行總數。我需要統計每一排中的所有總數以獲得總計。我將如何在Laravel中設置我的查詢?統計我的總數 - Laravel 5.2

我的訂單表:

My Orders Table

我的功能:

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')); 
    } 

回答

4

可以用雄辯的語言,這樣做就可以獲得列的總和。

$total = Orders::sum('total'); 
echo "<pre>"; 
    print_r($total); 
echo "<pre>"; 
+0

也可以。謝謝! – David

2

嘗試sum() aggrigate方法:

$count_total = DB::table('orders')->sum('total'); 
+0

哇,這是那麼容易,謝謝! – David