0
@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?

MDB
  • 339
  • 4
  • 19

1 Answers1

1

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
JS TECH
  • 1,556
  • 2
  • 11
  • 27
  • This returns an error : syntax error, unexpected token "else" – MDB Sep 23 '22 at 01:21
  • 1
    @MDB, if it doesn't work then you should use `foreach` separately. Also if you want to reuse your code you can isolate it, and call it like I showed in my updated answer. – JS TECH Sep 23 '22 at 01:40
  • Yeah, I ended up not forcing the whole block as a component. It makes it more complicated and not easily readable. Thanks – MDB Sep 23 '22 at 02:15
  • marking this as correct but the $value is actually dependent on the collection receive. If it is a franchiseTypes then it is $item->type. Else, it is a $item->category. Sorry I haven't mentioned the struct of the collections there. Thanks. – MDB Sep 23 '22 at 02:24