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

9

40

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?

@foreach($items as $item)
    <h4>{{ $item->program_name }}</h4>
@endforeach`
Roi answered 25/11, 2015 at 16:46 Comment(0)
E
12

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.

Edmead answered 25/11, 2015 at 16:52 Comment(2)
Both, yours and @gumma-mocciaro solutions work in my case. This one is shorter.Roi
Worked perfect for me !Pasturage
M
129

Laravel provides a $loop variable in foreach loops.

@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/10.x/blade#the-loop-variable

Malcah answered 16/10, 2016 at 0:50 Comment(3)
Exactly what I was looking for. Thanks for this!Denice
This should be the best answer.Secrecy
GREAT! And it still works on Laravel 5.8Elocution
E
12

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.

Edmead answered 25/11, 2015 at 16:52 Comment(2)
Both, yours and @gumma-mocciaro solutions work in my case. This one is shorter.Roi
Worked perfect for me !Pasturage
D
8

Just take the key value

@foreach($items as $index => $item)
    @if($index == 0)
        ...
    @endif
    <h4>{{ $item->program_name }}</h4>
@endforeach
Derail answered 25/11, 2015 at 16:52 Comment(2)
This will fail if the first item's index is not zero.Aviles
This will fail even if $items is not an array :).Derail
S
7

As of Laravel 7.25, Blade now includes a new @once component, so you can do it like this:

@foreach($items as $item)
    @once
    <h4>{{ $item->program_name }}</h4>  // Displayed only once
    @endonce
    // ... rest of looped output
@endforeach
Scend answered 28/1, 2021 at 16:17 Comment(0)
I
6

Laravel 7.* provides a first() helper function.

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

*Note that I'm not sure when this was introduced. So, it may not work on earlier versions.

It is only briefly mentioned in the documentation here.

Interlaken answered 23/7, 2020 at 10:5 Comment(0)
K
4

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

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

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

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

@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 special string.

Regards.

Krasnoyarsk answered 29/1, 2018 at 11:55 Comment(0)
G
1

if you need only the first element you can use @break inside your @foreach or @if.see example:

@foreach($media as $m)
    @if ($m->title == $loc->title) :
        <img class="card-img-top img-fluid" src="images/{{ $m->img }}">
          
        @break
    @endif
@endforeach
Grundyism answered 18/7, 2020 at 6:55 Comment(0)
J
0

you can do it by this way.

collect($users )->first();
Jap answered 11/1, 2022 at 12:53 Comment(0)
I
-2

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

@foreach($items as $item)
    @if($item == $items->first()) {{-- first item --}}
        <h4>{{$item->program_name}}</h4>
    @else
        <h5>{{$item->program_name}}</h5>
    @endif
@endforeach            
Inexpugnable answered 12/3, 2018 at 17:58 Comment(6)
The question is about the Blade templating system; not Laravel itself.Ataractic
The question is about LARAVEL Blade. Read the questionInexpugnable
Blade is the template system included in Laravel. OP isn’t asking about Laravel as a whole but rather Blade itself. Your answer doesn’t address OP’s question, as you can guess from the downvotes and the others’ answers.Ataractic
The question is : How to display something only for the first item from the collection in Laravel Blade template. There is LARAVEL in the question !!!Inexpugnable
There is "laravel blade" in the question. Not "laravel" only.Ataractic
The answer with best note quotes Laravel 5.3 !Inexpugnable

© 2022 - 2024 — McMap. All rights reserved.