转换为 Blade+Laravel 一个 while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48111958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:12:16  来源:igfitidea点击:

Translate to Blade+Laravel a while loop

laravelblade

提问by Poxxac

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

这段代码有效,但是在我学习Laravel的过程中,我想知道是否使用Blade+Laravel语法,可以更好地实现

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

Thanks

谢谢

回答by Rwd

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

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

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

在这种情况下,我建议使用 for 循环而不是 while 循环:

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

回答by Luca Di Lenardo

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

我不确定是最好的方法,但有效。

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

回答by fayaz mohammad

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

回答by Cedric

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

就在这里。模板就是为此而制作的,你可以看到文档是如何完成类似的事情的:laravel Blade : loops

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

回答by Amarnasan

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

回答by xok

The better solution would be to use @foreach

更好的解决方案是使用@foreach

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

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

@endforeach