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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community