Here is a quick question: Is there any way to optimise this code with a foreach loop or something like this?
@if(isset($tags[0]))
<span class="label label-primary">{{ $tags[0] }}</span>
@endif
@if(isset($tags[1]))
<span class="label label-primary">{{ $tags[1] }}</span>
@endif
@if(isset($tags[2]))
<span class="label label-primary">{{ $tags[2] }}</span>
@endif
@if(isset($tags[3]))
<span class="label label-primary">{{ $tags[3] }}</span>
@endif
@if(isset($tags[4]))
<span class="label label-primary">{{ $tags[4] }}</span>
@endif
@if(isset($tags[5]))
<span class="label label-primary">{{ $tags[5] }}</span>
@endif
@if(isset($tags[6]))
<span class="label label-primary">{{ $tags[6] }}</span>
@endif
@if(isset($tags[7]))
<span class="label label-primary">{{ $tags[7] }}</span>
@endif
@if(isset($tags[8]))
<span class="label label-primary">{{ $tags[8] }}</span>
@endif
@if(isset($tags[9]))
<span class="label label-primary">{{ $tags[9] }}</span>
@endif
Thank you and have a nice day/night !
You can do it simply like this :
@foreach ($tags as $tag)
<span class="label label-primary">{{ $tag }}</span>
@endforeach
You can use an @foreach
loop (as suggested by other answers) or an @for
loop. Use blade
syntax for control structures (preceeded by an @
symbol)
@for($i = 0; $i <= 9; $i++)
@if(isset($tags[$i]))
<span class="label label-primary">{{ $tags[$i] }}</span>
@endif
@endfor
Per the documentation , you can do @for
loops.
@for ($i = 0; $i < 10; $i++)
@if(isset($tags[$i]))
<span class="label label-primary">{{ $tags[$i] }}</span>
@endif
@endfor
@foreach ($tags as $data)
<span class="label label-primary">{{$data->property }}</span>
@endforeach
you can also use the for else in blade just see the laravel docs https://laravel.com/docs/5.5/blade
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.