October 13, 2015 19:36
I actually thought of this while answering a question on StackOverflow
When querying sums of columns, I typically do something like this:
$query = $account->transactions() ->select(\DB::raw('SUM(amount) AS total')) ->first(); $total = $query->total;
Here's another way to accomplish the same thing. Of course if you end up fetching thousands of rows you'll probably want to just let MySQL do the math for you, but it's way easier to read and cleaner.
$amounts = $account->transactions()->lists('amount'); $total = $amounts->sum();