Do not display variable value in the Laravel view

Asked

Viewed 41 times

1

    @foreach ($respostas as $resposta)

           @if($reclamacao->id == $resposta->reclamacao_id)
    
                <td>{{$resposta->resposta}}</td>
    
                {{$i++}}
    
           @endif

    @endforeach

When I run this code snippet, the increment values of the variable $i are displayed in the view, and I’m only using it for control. Does anyone know how to stop displaying these values?

  • I recommend reading https://laravel.com/docs/8.x/blade#the-loop-variable

2 answers

1

to run a php command without displaying it use @php

@foreach ($respostas as $resposta)

       @if($reclamacao->id == $resposta->reclamacao_id)

            <td>{{$resposta->resposta}}</td>

            @php 
            $i++; 
            @endphp

       @endif

@endforeach

But look, you’re using a foreach, so it doesn’t depend on an increment. if you are using the increment in another place that is not visible in the code that provided all right.

  • Thanks buddy, solved my problem.

0

It has already been answered, but you could also use a simpler way, using @php.

Behold:

@foreach ($respostas as $resposta)
        @if($reclamacao->id == $resposta->reclamacao_id)
            <td>{{$resposta->resposta}}</td>
            @php($i++)
        @endif
@endforeach

In addition to accepting an expression within @php until @endphp, it is also possible to use only the short call as shown above.

Browser other questions tagged

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