Translate to Blade+Laravel a while loop

后端 未结 6 1405
無奈伤痛
無奈伤痛 2021-01-26 12:54

This code works, however in my learning of Laravel, I want to know if using Blade+Laravel syntax, can be better implemented



        
相关标签:
6条回答
  • 2021-01-26 13:24
    @for ($i = 0; $i <= (5 - $post->images->count()); $i++) 
       <div class="col"> </div>
    @endfor
    
    0 讨论(0)
  • 2021-01-26 13:29

    https://laravel.com/docs/5.5/blade#loops

    I would suggest using a for loop instead of a while loop in this case:

    @for ($i = 1; $i <= (5 - $post->images->count()); $i++)
        <div class="col"> </div>
    @endfor
    
    0 讨论(0)
  • 2021-01-26 13:30
    @php 
        $i = 0; 
    @endphp 
    @while (++$i <= (5 - $post->images->count())) 
        <div class="col"> 
        </div>
    @endwhile
    
    0 讨论(0)
  • 2021-01-26 13:34

    I'm not sure is the best way to do, but works.

    <?php $z = 0; ?>
    @while ($z < 3)
      {{ "test".$z }} 
      <?php $z++ ?>
    @endwhile
    
    0 讨论(0)
  • 2021-01-26 13:44

    Yes, there is. Templating is made just for that, you can see how similar things are done with the docs : laravel blade : loops

    @for ($i = 0; $i <  $post->images->count()); $i++)
        <div class="col"> </div>
    @endfor
    
    0 讨论(0)
  • 2021-01-26 13:46

    The better solution would be to use @foreach

    @foreach( $image as $post->images )
    
       <div class="col"> </div>
    
    @endforeach
    
    0 讨论(0)
提交回复
热议问题