David David - 2 years ago 93
PHP Question

Count my totals up - Laravel 5.2

I have a "orders" table that holds the totals in each row for every order in my shop. I need to count all my totals from every single row to get a grand total. How would I set up my query in Laravel?

My Orders Table:

My Orders Table

My function:

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();

// Get all the carts in DB
$carts = Cart::all();

return view('admin.pages.index', compact('orders', 'carts', 'count_products'));

Answer Source

you can use Eloquent way, by doing this you can get total sum of column.

$total = Orders::sum('total');
echo "<pre>";
echo "<pre>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download