转换为Blade + Laravel一会儿循环

问题描述 投票:1回答:5

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

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

谢谢

laravel blade
5个回答
0
投票

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

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

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

1
投票

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

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

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

0
投票

就在这里。模板化就是为了这个,您可以看到文档的相似之处:laravel blade : loops

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

0
投票

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

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

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

@endforeach
© www.soinside.com 2019 - 2024. All rights reserved.