David
David

Reputation: 1997

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();
        dd($count_total); 

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


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

Upvotes: 2

Views: 1501

Answers (2)

Qazi
Qazi

Reputation: 5135

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

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

Upvotes: 4

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

Try sum() aggrigate method:

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

Upvotes: 2

Related Questions