laravellaravel-components

Laravel : Conditional block for the @foreach() in blade


@foreach (($loop->index == 3 ? FILTER_OPTION_NEW_EXISTING : ($loop->index == 4 ? $franchiseTypes : $categories)) as ($loop->index == 3 ? $key => $value : $item))

How can I attain this one? The first part of the @foreach depends on the loop->index number. My error comes after the 'AS', I think.

I am receiving an error :

syntax error, unexpected token "<"

This is the whole block:

<div class="row mt-3">
      @foreach (BRAND_FILTER_LISTING as $keyFilter => $filter)
        @if ($loop->index > 2)
          <div class="col-md-4 filter-container">
            <h5>{{ $filter['filterLabel'] }}(<span class="{{ $keyFilter }}-selected-count filter-count">{{(count(preg_grep('/^' . $keyFilter .'/i', $activeListFilters)))}}</span>/
              @if ($loop->index == 4)
                {{ count($franchiseTypes) }})
              @elseif ($loop->index == 5)
                {{ count($categories) }})
              @else
                {{ count(FILTER_OPTION_NEW_EXISTING) }})
              @endif
            </h5>
            <div class="filter-options">
                @foreach (($loop->index == 3 ? FILTER_OPTION_NEW_EXISTING : ($loop->index == 4 ? $franchiseTypes : $categories)) as ($loop->index == 3 ? $key => $value : $item))
                <div class="form-check">
                  <input class="form-check-input filter-checkbox {{$keyFilter}}-checkbox" type="checkbox" value="{{$loop->index == 3 ? $key : $item->id}}" name="{{$keyFilter}}[]" id="{{$keyFilter}}-{{$loop->index == 3 ? $key : $item->id}}" data-filtertype="{{$keyFilter}}" 
                    @if(in_array('{{$keyFilter}}-' . $loop->index == 3 ? $key : $item->id, $activeListFilters))
                        checked
                    @endif
                  >
                  <label class="form-check-label" for="{{$keyFilter}}-{{ $loop->index == 3 ? $key : $item->id }}">
                    {{$loop->index == 3 ? $value : $item->type}}
                  </label>
                </div>
                @endforeach
            </div>

            <x-brand.listing.filter-action-buttons filterType="{{$keyFilter}}"/>
          </div>
        @endif
      @endforeach
    </div>

Is there any way to do this?


Solution

  • For nested foreach, to access parent $loop->index, you must use $loop->parent->index.

    And for your syntax error, you can do something like this:

    @if ($loop->index == 3)
        @foreach (FILTER_OPTION_NEW_EXISTING as $key => $value)
            <div class="form-check">
                <input class="form-check-input filter-checkbox {{ $keyFilter }}-checkbox" type="checkbox"
                    value="{{ $key }}" name="{{ $keyFilter }}[]" id="{{ $keyFilter }}-{{ $key }}"
                    data-filtertype="{{ $keyFilter }}" @if (in_array('{{ $keyFilter }}-' . $key, $activeListFilters)) checked @endif>
                <label class="form-check-label" for="{{ $keyFilter }}-{{ $key }}">{{ $value }}</label>
            </div>
        @endforeach
    @else
        @foreach ($loop->index == 4 ? $franchiseTypes : $categories as $item)
            <div class="form-check">
                <input class="form-check-input filter-checkbox {{ $keyFilter }}-checkbox" type="checkbox"
                    value="{{ $item->id }}" name="{{ $keyFilter }}[]"
                    id="{{ $keyFilter }}-{{ $item->id }}" data-filtertype="{{ $keyFilter }}"
                    @if (in_array('{{ $keyFilter }}-' . $item->id, $activeListFilters)) checked @endif>
                <label class="form-check-label" for="{{ $keyFilter }}-{{ $item->id }}">{{ $item->type }}</label>
            </div>
        @endforeach
    @endif
    

    Or,

    template.blade.php

    <div class="form-check">
        <input class="form-check-input filter-checkbox {{ $keyFilter }}-checkbox" type="checkbox"
            value="{{ $key }}" name="{{ $keyFilter }}[]"
            id="{{ $keyFilter }}-{{ $key }}" data-filtertype="{{ $keyFilter }}"
            @if (in_array('{{ $keyFilter }}-' . $key, $activeListFilters)) checked @endif>
        <label class="form-check-label" for="{{ $keyFilter }}-{{ $key }}">{{ $value }}</label>
    </div>
    

    Then,

    @if ($loop->index == 3)
        @foreach (FILTER_OPTION_NEW_EXISTING as $key => $value)
            @include('template', [ 'activeListFilters' => $activeListFilters, 'keyFilter' => $keyFilter, 'key' => $key, 'value' => $value ])
        @endforeach
    @else
        @foreach ($loop->index == 4 ? $franchiseTypes : $categories as $item)
            @include('template', [ 'activeListFilters' => $activeListFilters, 'keyFilter' => $keyFilter, 'key' => $item->id, 'value' => $item->type ])
        @endforeach
    @endif