问题
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