2

I'm using laravel framework, very newbie with it. I have multiple records in my php application, something like this:

"[{"balance":20,"account_number":"1"},
{"balance":80,"account_number":"1"}
{"balance":20,"account_number":"2"}]"

I need to SUM the total balance of account_number.

So it should be:

account_number1 - balance 100
account_number2 - balance 20

I have tried

>>> $orders = DB::table('demo_trades')->select('balance', DB::raw('SUM(balance)'))->groupBy('account_number')->get()->toJson();

How I can do it?

1
  • You can use aggregate function predefined in DB like this DB::table('demo_trades')->sum('balance')->groupBy('account_number')->get()->toJson(); Commented Dec 27, 2017 at 10:56

2 Answers 2

1

Ok thx guys how helped me

i did it

$users = DB::table('demo_trades')->select(DB::raw('sum(balance) as balance, account_number'))->groupBy('account_number')->get()->toJson();

Sign up to request clarification or add additional context in comments.

Comments

0

You need Laravel Aggregates queries

$orders = DB::table('demo_trades')
        ->sum('demo_trades.balance')
        ->groupBy('account_number')->get();

1 Comment

@BillalAhmed i have got PHP Error: Call to a member function groupBy() on float on line 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.