【问题标题】:How to display something only for the first item from the collection in Laravel Blade template如何仅在 Laravel Blade 模板中为集合中的第一项显示内容
【发布时间】:2015-11-25 16:46:51
【问题描述】:

我在 Blade 模板中有一个 @foreach 循环,需要对集合中的第一项应用特殊格式。如何添加条件来检查这是否是第一项?

@foreach($items as $item)
    <h4>{{ $item->program_name }}</h4>
@endforeach`

【问题讨论】:

    标签: php laravel-5.1 laravel-blade


    【解决方案1】:

    Laravel 5.3foreach 循环中提供了一个 $loop 变量。

    @foreach ($users as $user)
        @if ($loop->first)
            This is the first iteration.
        @endif
    
        @if ($loop->last)
            This is the last iteration.
        @endif
    
        <p>This is user {{ $user->id }}</p>
    @endforeach
    

    文档:https://laravel.com/docs/5.3/blade#the-loop-variable

    【讨论】:

    • 正是我想要的。谢谢!
    【解决方案2】:

    从 Laravel 7.25 开始,Blade 现在包含一个新的 @once 组件,因此您可以这样做:

    @foreach($items as $item)
        @once
        <h4>{{ $item->program_name }}</h4>  // Displayed only once
        @endonce
        // ... rest of looped output
    @endforeach
    

    【讨论】:

      【解决方案3】:

      只取键值

      @foreach($items as $index => $item)
          @if($index == 0)
              ...
          @endif
          <h4>{{ $item->program_name }}</h4>
      @endforeach
      

      【讨论】:

      • 如果第一项的索引不为零,这将失败。
      • 即使$items 不是数组也会失败:)。
      【解决方案4】:

      Laravel 7.* 提供了一个 first() 辅助函数。

      {{ $items->first()->program_name }}
      

      *请注意,我不确定这是什么时候引入的。因此,它可能不适用于早期版本。

      documentation here中只是简单提及。

      【讨论】:

        【解决方案5】:

        Liam Wiltshire 回答的主要问题是性能,因为:

        1. reset($items) 在每个循环中一次又一次地倒回 $items 集合的指针...总是得到相同的结果。

        2. $itemreset($item) 的结果都是对象,所以 $item == reset($items) 需要对其属性进行全面比较...需要更多的处理器时间。

        一种更高效、更优雅的方法——香农建议s——是使用 Blade 的 $loop 变量:

        @foreach($items as $item)
            @if ($loop->first) First Item: @endif
            <h4>{{ $item->program_name }}</h4>
        @endforeach
        

        如果您想对第一个元素应用特殊格式,那么也许您可以执行类似的操作(使用三元条件运算符 ?: ):

        @foreach($items as $item)
            <h4 {!! $loop->first ? 'class="special"': '' !!}>{{ $item->program_name }}</h4>
        @endforeach
        

        注意使用{!!!!} 标记而不是{{ }} 符号以避免特殊 字符串周围的双引号的html 编码.

        问候。

        【讨论】:

          【解决方案6】:

          如果您只需要第一个元素,您可以在 @foreach@if 中使用 @break。参见示例:

          @foreach($media as $m)
              @if ($m->title == $loc->title) :
                  <img class="card-img-top img-fluid" src="images/{{ $m->img }}">
                    
                  @break
              @endif
          @endforeach
          

          【讨论】:

            【解决方案7】:

            苏豪区,

            最快的方法是将当前元素与数组中的第一个元素进行比较:

            @foreach($items as $item)
                @if ($item == reset($items )) First Item: @endif
                <h4>{{ $item->program_name }}</h4>
            @endforeach
            

            否则,如果它不是关联数组,您可以按照上面的答案检查索引值 - 但如果数组是关联的,那将不起作用。

            【讨论】:

            • 你的和@gumma-mocciaro 的解决方案都适用于我的情况。这个比较短。
            • 非常适合我!
            【解决方案8】:

            要在 Laravel 中获取集合的第一个元素,您可以使用:

            @foreach($items as $item)
                @if($item == $items->first()) {{-- first item --}}
                    <h4>{{$item->program_name}}</h4>
                @else
                    <h5>{{$item->program_name}}</h5>
                @endif
            @endforeach            
            

            【讨论】:

            • 问题是关于刀片模板系统;不是 Laravel 本身。
            • 问题是关于 LARAVEL Blade 的。阅读问题
            • Blade 是 Laravel 中包含的模板系统。 OP 不是询问整个 Laravel,而是询问 Blade 本身。您的回答没有解决 OP 的问题,您可以从反对票和其他人的回答中猜到。
            • 问题是:如何仅在 Laravel Blade 模板中为集合中的第一项显示内容。问题里有LARAVEL!!!
            • 问题中有“laravel Blade”。不仅仅是“laravel”。
            【解决方案9】:

            你可以这样做。

            collect($users )->first();
            

            【讨论】:

              猜你喜欢
              • 2017-01-26
              • 2015-07-15
              • 2013-06-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-06-28
              • 2015-04-25
              • 2021-08-18
              相关资源
              最近更新 更多