php 如何仅显示 Laravel Blade 模板中集合中的第一项的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33922028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:39:23  来源:igfitidea点击:

How to display something only for the first item from the collection in Laravel Blade template

phplaravel-5.1laravel-blade

提问by SoHo

I have a @foreach loop in the Blade template and need to apply special formatting to the first item in the collection. How do I add a conditional to check if this is the first item?

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

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

采纳答案by Liam Wiltshire

SoHo,

苏豪,

The quickest way is to compare the current element with the first element in the array:

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

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

Or otherwise, if it's not an associative array, you could check the index value as per the answer above - but that wouldn't work if the array is associative.

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

回答by Shannon Matthews

Laravel 5.3provides a $loopvariable in foreachloops.

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

@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

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

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

回答by Gumma Mocciaro

Just take the key value

只取键值

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

回答by Omar Vargas

The major problem with Liam Wiltshire's answer is the performance because:

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

  1. reset($items)rewind the pointer of $itemscollection again and again at each loop... always with then same result.

  2. Both $itemand the result of reset($item)are objects, so $item == reset($items)requires a full comparison of its attributes... demanding more processor time.

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

  2. 这两个$项目和结果复位($项目)都是对象,所以$项目==复位($项目),需要其属性的全面比较苛刻...更多的处理器时间。

A more efficient and elegant way to do that -as Shannon suggests- is to use the Blade's $loopvariable:

一种更有效、更优雅的方法 -正如香农建议的那样 -是使用 Blade 的$loop变量:

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

If you want to apply a special format to the first element, then maybe you could do something like (using the ternary conditional operator ?:):

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

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

Note the use of {!!and !!}tags instead of {{}}notation to avoid html encoding of the double quotes around of specialstring.

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

Regards.

问候。

回答by DBga

To get the first element of a collection in Laravel, you can use :

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

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