I want this output
id | jml | total |
---|---|---|
1 | 1000 | 1000 |
1 | 100 | 1100 |
2 | 400 | 400 |
2 | 300 | 700 |
but when i try my output is
id | jml | total |
---|---|---|
1 | 1000 | 1000 |
1 | 100 | 100 |
2 | 400 | 400 |
2 | 300 | 300 |
I try this to take sum of column
$total = DB::table('logs')
->groupBy('id')
->sum('jml');
and this to add new input
$total += $request->jml;
$total = (string)$total;
3
Answers
Try this using an laravel eloquent model
or it might work using the laravel DB class like this
The above answer should do all ID numbers even duplicate ID numbers.
However you can also use this version which might be easier to understand and verify.
The above code should work for all ID’s even duplicate ID numbers.
However you can also do it this way.