这段代码有效,但在我学习Laravel时,我想知道是否使用Blade + Laravel语法,可以更好地实现
<?php
$i = 1;
while ($i <= (5 - $post->images->count())) {
echo '<div class="col"> </div>';
$i++;
}
?>
谢谢
https://laravel.com/docs/5.5/blade#loops
在这种情况下,我建议使用for循环而不是while循环:
@for ($i = 1; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
@endfor
我不确定这是最好的方法,但有效。
<?php $z = 0; ?>
@while ($z < 3)
{{ "test".$z }}
<?php $z++ ?>
@endwhile
@for ($i = 0; $i <= (5 - $post->images->count()); $i++)
<div class="col"> </div>
@endfor
就在这里。模板化就是为了这个,您可以看到文档的相似之处:laravel blade : loops
@for ($i = 0; $i < $post->images->count()); $i++)
<div class="col"> </div>
@endfor
更好的解决方案是使用@foreach
@foreach( $image as $post->images )
<div class="col"> </div>
@endforeach