This query works fine in phpMyAdmin reflecting the accumulated balance
SET @acumulador=0;
SELECT
concepto,
cargo,
abono,
@acumulador:=(cargo - abono + @acumulador) AS saldo
FROM poliza_detalles
WHERE cuenta_id = 1
But how to do that query in Laravel's Query Builder ORM?
I have the following query:
->select('tp.descripcion', 'pd.cuenta_id', 'pd.concepto', 'pd.cargo', 'pd.abono')
->where('pd.cuenta_id', '=', 1)
->groupBy('pd.id')
->get();
View:
@foreach ($auxBancos as $aux)
@php
$saldo = $aux->cargo - $aux->abono
@endphp
<tr>
<td>{{ $aux->concepto }}</td>
<td>{{ $aux->cargo }}</td>
<td>{{ $aux->abono }}</td>
<td>{{ $saldo }} </td>
</tr>
@endforeach
It shows me the following result, which is not what I want:
CARGO ABONO SALDO
100 0 100
0 50 -50
0 10 -10
30 0 30
What I expect:
CARGO ABONO SALDO
100 0 100
0 50 50
0 10 40
30 0 70
You could do it like this:
PS: You should remove the calculation of the
saldo
.