How to add values in the middle of a foreach?

Asked

Viewed 236 times

-1

I have a foreach which mounts several tables, but has a td in my table he is the sum of others td.

I tried to do with Javascript and put together some PHP, but without success.

The field is the last of the tbody, what has the total date

@foreach($lancamentos as $lancamento)
@if($lancamento['lojas']['id'] != $id_anterior)
<?php  
    $id_anterior = $lancamento['lojas']['id'];
?> 
<table class="table table-sm table-striped table-bordered" id="{{$lancamento['lojas']['id']}}">
    <thead>
        <tr>
            <td colspan="7" style="text-align: center;"><b>Loja:</b> {{$lancamento['lojas']['nome']}}</td>
        </tr>
        <tr>
            <td>Boleta</td>
            <td>Romaneio</td>
            <td>Cliente</td>
            <td>Data da Compra</td>
            <td>Data de Vencimento</td>
            <td>Valor</td>
            <td>Valor a receber</td>
            <td>total</td>
        </tr>
    </thead>
    <tbody>

    @endif

        <tr>
            <td>{{$lancamento['boleta']}}</td>
            <td>{{$lancamento['romaneio']}}</td>
            <td>{{$lancamento['cliente']}}</td>
            <td>{{$lancamento['data_compra']}}</td>
            <td>{{$lancamento['data_vencimento']}}</td>
            <td data-valor="{{$lancamento['lojas']['id']}}">R$ {{$lancamento['valor']}}</td>
            <td>R$ {{$lancamento['valor'] * ($lancamento['lojas']['comissao']/100)}}</td>   
            <td data-total="{{$lancamento['lojas']['id']}}">{{ $lancamento['valor'] }}</td>
        </tr>
        @if($lancamento['lojas']['id'] != $id_anterior)
    </tbody>
</table>
@endif
@endforeach
  • Explain better what you want. The total would be the sum of the "value" and the "receivable"?

  • No, the "total" would be the sum of the "value"

1 answer

0

Although not recommended, it is possible to use tags @php and @endphp within your view.

Could be done in this case something like:

...
@php
  $total += $lancamento['valor'];
@endphp
<td data-total="{{$lancamento['lojas']['id']}}">{{ $total }}</td>

Remember to start the variable total out of its loop equal to 0.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.