Search code examples
phplaravel-5chunking

Laravel 5.4 checking if chunk is first chunk


I'm trying to set a condition check whether the chunk is the first batch of chunk or not.

Anyone know how to check what the chunk id or something similar for example:

@foreach($item->chunk(3) as $item_chunk
    @if(chunk is first chunk)
        <div class="carousel-item active">
    @else
        <div class="carousel-item">
    @endif

    @foreach($item_chunk as $item)
        {{-- Do something --}}
    @endforeach 
@endforeach

Appreciate it if someone knows of a way to check whether the chunked list is first or not.


Solution

  • You can check loop using $loop variable.

    @foreach($item->chunk(3) as $item_chunk)
        @if($loop->first)
            <div class="carousel-item active">
        @else
            <div class="carousel-item">
        @endif
    
        @foreach($item_chunk as $item)
            {{-- Do something --}}
        @endforeach 
    @endforeach