Translate to Blade+Laravel a while loop

巧了我就是萌 提交于 2020-02-16 07:35:00

问题


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

<?php
    $i = 1;
    while ($i <= (5 - $post->images->count())) {
        echo '<div class="col"> </div>';
        $i++;
    }
    ?>

Thanks


回答1:


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



回答2:


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

<?php $z = 0; ?>
@while ($z < 3)
  {{ "test".$z }} 
  <?php $z++ ?>
@endwhile



回答3:


@php 
    $i = 0; 
@endphp 
@while (++$i <= (5 - $post->images->count())) 
    <div class="col"> 
    </div>
@endwhile



回答4:


@for ($i = 0; $i <= (5 - $post->images->count()); $i++) 
   <div class="col"> </div>
@endfor



回答5:


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



回答6:


The better solution would be to use @foreach

@foreach( $image as $post->images )

   <div class="col"> </div>

@endforeach


来源:https://stackoverflow.com/questions/48111958/translate-to-bladelaravel-a-while-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!