Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 2 years ago.
0

You've simply got it backwards.

$balance = DB::table('data')->where('user_id' '=' $id)->sum('balance');

table('table') <- returns a query builder, with things like 'where', 'orWhere', 'groupBy', 'orderBy', etc;

where('some_column','some_operator','some_value') <- returns a query builder also.

sum('column name') <- returns a number. So naturally it can only do what numbers can do.

It's a little backwards from sql. You run your filters/sorts/groupings first, then when you run a fetching command ("sum","count","first","get", etc;) that is the last thing you can do as far as the query is concerned. At that point, the rest is php.

Last updated 9 years ago.
0

Ahhh ok, that makes sense. Thank you.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

ericbher ericbher Joined 3 Dec 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.