@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 >
@foreach (BRAND_FILTER_LISTING as $keyFilter => $filter)
@if ($loop->index > 2)
<div >
<h5>{{ $filter['filterLabel'] }}(<span >{{(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 >
@foreach (($loop->index == 3 ? FILTER_OPTION_NEW_EXISTING : ($loop->index == 4 ? $franchiseTypes : $categories)) as ($loop->index == 3 ? $key => $value : $item))
<div >
<input 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 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?
CodePudding user response:
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 >
<input type="checkbox"
value="{{ $key }}" name="{{ $keyFilter }}[]" id="{{ $keyFilter }}-{{ $key }}"
data-filtertype="{{ $keyFilter }}" @if (in_array('{{ $keyFilter }}-' . $key, $activeListFilters)) checked @endif>
<label for="{{ $keyFilter }}-{{ $key }}">{{ $value }}</label>
</div>
@endforeach
@else
@foreach ($loop->index == 4 ? $franchiseTypes : $categories as $item)
<div >
<input type="checkbox"
value="{{ $item->id }}" name="{{ $keyFilter }}[]"
id="{{ $keyFilter }}-{{ $item->id }}" data-filtertype="{{ $keyFilter }}"
@if (in_array('{{ $keyFilter }}-' . $item->id, $activeListFilters)) checked @endif>
<label for="{{ $keyFilter }}-{{ $item->id }}">{{ $item->type }}</label>
</div>
@endforeach
@endif
Or,
template.blade.php
<div >
<input type="checkbox"
value="{{ $key }}" name="{{ $keyFilter }}[]"
id="{{ $keyFilter }}-{{ $key }}" data-filtertype="{{ $keyFilter }}"
@if (in_array('{{ $keyFilter }}-' . $key, $activeListFilters)) checked @endif>
<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