Search code examples
phplaravelfor-loopvariableslaravel-blade

Declaring a variable in blade php (LARAVEL UPDATED)


I have a simple project where Im using laravel with blade. I recive an array called 'tablero' and I have to see all it values. I have this code:

enter image description here

But I received this error: Undefined constant "i" (View: /var/www/html/resources/views/tablero2.blade.php) How do i define i?


Solution

  • Loops

    In addition to conditional statements, Blade provides simple directives for working with PHP's loop structures. Again, each of these directives functions identically to their PHP counterparts:

    Solution one:

    @for ($i = 0; $i < count($tablero); $i++)
        <p> {{ $tablero[$i] }} </p>
    @endfor
    

    Solution two:

    @foreach ($tablero as $tab)
        <p> {{ $tab }} </p>
    @endforeach
    

    Solution three:

    
    @php($i = 0)
    
    @while ($i < count($tablero))
        <p> {{ $tablero[$i] }} </p>
        @php($i++)
    @endwhile