0

I'm using laravel blade. I try to print array of data as a column in a table. then, I put a conditional expression inside the loop to avoid printing the same value in the same column. After that my data printed twice in the first iteration.Here is the code

    {{ $prevKomponen = null }}
    {{ $prevKegiatan = null }}
    @foreach($data as $key => $value)
        <tr>

            @if ($prevKomponen != $value['komponen'])

                <td>{{ $value['komponen'] }}***</td>  

            @else

                <td>-------</td>

            @endif    

            {{ $prevKomponen = $value['komponen'] }}

            <td>{{$value['kegiatan']}}</td>
            <td>{{$value['subkegiatan']}}</td>
            <td>{{$value['rincian']}}</td>
            <td>{{$value['jumlah']}}</td>
            <td>{{$value['harga']}}</td>
            <td>{{$value['total_harga']}}</td>
        </tr>
    @endforeach

And here is the result. Data in column 1 is also printed in column 2

enter image description here

Any help to address this problem ?

  • Maybe because your affectation is also returning the value so it's printed ... Why are you doing ``{{ $prevKomponen = $value['komponen'] }}`` ? – Ko2r Nov 23 '17 at 10:47
  • @Ko2r I need it to be a comparison for data in the next row. If the value of "komponen" in the next row is the same as previous row, then do not print it – Nicky Prabowo Nov 23 '17 at 10:50
  • How you want result to be? Can you provide sample? – Ndroid21 Nov 24 '17 at 05:26

2 Answers2

0

you have missing a td, try this snipet in your if condition

@if ($prevKomponen != $value['komponen'])

         <td>{{ $value['komponen'] }}***</td>  

        @else

                <td>-------</td>

       @endif    

          <td>{{ $prevKomponen = $value['komponen'] }}</td>
Riajul Islam
  • 1,425
  • 15
  • 19
  • `{{ $prevKomponen = $value['komponen'] }}` is not meant to be displayed. This is only to assign value to `$prevKomponen` – Nicky Prabowo Nov 23 '17 at 11:35
0

This must be caused by https://laravel.com/docs/5.5/blade#displaying-data. It says "You may display data passed to your Blade views by wrapping the variable in curly braces".

So, when you hit these line:

{{ $prevKomponen = $value['komponen'] }}

It will display as output.

But, if you want to ignore it, you can add '@' symbol, just like this:

@{{ $prevKomponen = $value['komponen'] }}

'@' symbol to inform the Blade rendering engine an expression should remain untouched. Hopefully this will help you.

Yasin Junet
  • 108
  • 1
  • 1
  • 6
  • how about using php tag instead of {{ }} ? or move your code {{ $prevKomponen = $value['komponen'] }} into line before endforeach loop? – Yasin Junet Nov 26 '17 at 12:04
  • 1
    moving ` {{ $prevKomponen = $value['komponen'] }}` right before @endforeach solve the problem.Thank you – Nicky Prabowo Nov 28 '17 at 04:02