Hello, I am doing a query in which it returns 3 numeric fields, these fields are:
- total_perceptions
- total_deductions
- total_pantry_vouchers
I want to add these fields and then return only what is different from 0, I am trying the following way in Laravel.
`$payrolls = ProcessedPayroll::where('payroll_id', $id)->sum('total_perceptions' + 'total_deductions')->get()`;
Also, try this way but no results
$payrolls = ProcessedPayroll::where('payroll_id', $id)->sum('total_perceptions + total_deductions')->get();
Any idea or example you can give me?
The results I'm looking for are like this.
| id | total_perceptions| total_deductions|total_pantry_vouchers|Total
+----+------------------+------------------+------------------+--------
| 1 | 5 5 | 0 |10 + |
+----+------------------+-------------------+------------------+-------
Hello, in your case I think the following fragment can help you, in which you add each column, at the end you save it in the variable SumTotal
I hope this helps you
The way I managed to solve my question is this way, I'm not sure it's the right solution but it works as expected.
I hope someone else finds it helpful, you can't use the sum method, since it joins all the fields and throws them into a single record.