title: Laravel : Conditional block for the @foreach() in blade
body: <pre><code>@foreach (($loop-&gt;index == 3 ? FILTER_OPTION_NEW_EXISTING : ($loop-&gt;index == 4 ? $franchiseTypes : $categories)) as ($loop-&gt;index == 3 ? $key =&gt; $value : $item))
  </code></pre>
  <p>How can I attain this one?
  The first part of the @foreach depends on the loop-&gt;index number. My error comes after the 'AS', I think.</p>
  <p>I am receiving an error :</p>
  <pre><code>syntax error, unexpected token &quot;&lt;&quot;
  </code></pre>
  <p>This is the whole block:</p>
  <pre><code>&lt;div class=&quot;row mt-3&quot;&gt;
        @foreach (BRAND_FILTER_LISTING as $keyFilter =&gt; $filter)
          @if ($loop-&gt;index &gt; 2)
            &lt;div class=&quot;col-md-4 filter-container&quot;&gt;
              &lt;h5&gt;{{ $filter['filterLabel'] }}(&lt;span class=&quot;{{ $keyFilter }}-selected-count filter-count&quot;&gt;{{(count(preg_grep('/^' . $keyFilter .'/i', $activeListFilters)))}}&lt;/span&gt;/
                @if ($loop-&gt;index == 4)
                  {{ count($franchiseTypes) }})
                @elseif ($loop-&gt;index == 5)
                  {{ count($categories) }})
                @else
                  {{ count(FILTER_OPTION_NEW_EXISTING) }})
                @endif
              &lt;/h5&gt;
              &lt;div class=&quot;filter-options&quot;&gt;
                  @foreach (($loop-&gt;index == 3 ? FILTER_OPTION_NEW_EXISTING : ($loop-&gt;index == 4 ? $franchiseTypes : $categories)) as ($loop-&gt;index == 3 ? $key =&gt; $value : $item))
                  &lt;div class=&quot;form-check&quot;&gt;
                    &lt;input class=&quot;form-check-input filter-checkbox {{$keyFilter}}-checkbox&quot; type=&quot;checkbox&quot; value=&quot;{{$loop-&gt;index == 3 ? $key : $item-&gt;id}}&quot; name=&quot;{{$keyFilter}}[]&quot; id=&quot;{{$keyFilter}}-{{$loop-&gt;index == 3 ? $key : $item-&gt;id}}&quot; data-filtertype=&quot;{{$keyFilter}}&quot; 
                      @if(in_array('{{$keyFilter}}-' . $loop-&gt;index == 3 ? $key : $item-&gt;id, $activeListFilters))
                          checked
                      @endif
                    &gt;
                    &lt;label class=&quot;form-check-label&quot; for=&quot;{{$keyFilter}}-{{ $loop-&gt;index == 3 ? $key : $item-&gt;id }}&quot;&gt;
                      {{$loop-&gt;index == 3 ? $value : $item-&gt;type}}
                    &lt;/label&gt;
                  &lt;/div&gt;
                  @endforeach
              &lt;/div&gt;
  
              &lt;x-brand.listing.filter-action-buttons filterType=&quot;{{$keyFilter}}&quot;/&gt;
            &lt;/div&gt;
          @endif
        @endforeach
      &lt;/div&gt;
  </code></pre>
  <p>Is there any way to do this?</p>