dsgoj7457
dsgoj7457
2016-04-24 07:57

有没有办法在foreach循环中只生成一个项目一次?

There is one element I want to show only once inside this foreach loop. I was wondering if this was possible or if I would need to change my html/css structure for it with Laravel. I only want to show the icon here once.

example.blade.php :

@foreach ($items as $item)
    <p>
        {{ $item }}
        <i class="fa fa-times><!-- this only once --></i>
    </p>
@endforeach
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doubi1818 doubi1818 5年前

    Answer from the comments worked best for me :

    @foreach ($items as $key => $item)
         {{ $item }}
         @if($key == 0)
             <i class="fa fa-times><!-- this only once --></i>
         @endif
    @endforeach
    
    点赞 评论 复制链接分享
  • dqyym3667 dqyym3667 5年前

    You can do this:

    @foreach ($items as $item)
        <p>
            {{ $item }}
            <i class="fa fa-times>
                @if(!isset($iconShown))
                {{ $iconShown = true }}
                    DisplayIconHere
                @endif
            </i>
        </p>
    @endforeach
    
    点赞 评论 复制链接分享

为你推荐