2016-04-24 49 views
0

在这个foreach循环中,我只想显示一个元素。我想知道如果这是可能的,或者如果我需要改变我的HTML/CSS结构与Laravel。我只想在这里显示图标一次。有没有办法在foreach循环中产生一个项目一次?

example.blade.php:

@foreach ($items as $item) 
    <p> 
     {{ $item }} 
     <i class="fa fa-times><!-- this only once --></i> 
    </p> 
@endforeach 
+0

只是把它拿出来的的foreach? – Doug

+1

执行'@foreach($ items as $ key => $ item)',并且只在'$ key == 0'时输出图标。 – Chris

+0

您可以发布这个答案,让我可以接受它:)谢谢 –

回答

0

从评论答案对我来说最好的工作:

@foreach ($items as $key => $item) 
    {{ $item }} 
    @if($key == 0) 
     <i class="fa fa-times><!-- this only once --></i> 
    @endif 
@endforeach 
+0

要求克里斯发布它作为一个答案,以便他可以承认。 – RST

0

你可以这样做:

@foreach ($items as $item) 
    <p> 
     {{ $item }} 
     <i class="fa fa-times> 
      @if(!isset($iconShown)) 
      {{ $iconShown = true }} 
       DisplayIconHere 
      @endif 
     </i> 
    </p> 
@endforeach 
相关问题