【问题标题】:Laravel : Conditional block for the @foreach() in bladeLaravel:刀片中 @foreach() 的条件块
【发布时间】:2022-09-22 23:45:10
【问题描述】:
@foreach (($loop->index == 3 ? FILTER_OPTION_NEW_EXISTING : ($loop->index == 4 ? $franchiseTypes : $categories)) as ($loop->index == 3 ? $key => $value : $item))

我怎样才能得到这个? @foreach 的第一部分取决于循环-> 索引号。我认为我的错误出现在 \'AS\' 之后。

我收到一个错误:

syntax error, unexpected token \"<\"

这是整个块:

<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>

有没有办法做到这一点?

  • 你不能在foreach() 中使用$loop
  • @JSTECH 实际上, $loop 来自外部 foreach。
  • 我在你的代码中没有看到 foreach 之外的另一个 foreach,那它是怎么来的?
  • 对不起,我刚刚更新了块。
  • 对于嵌套的 foreach,要访问父 $loop,您使用了 $loop-&gt;parent-&gt;index

标签: laravel laravel-components


【解决方案1】:

对于嵌套的 foreach,要访问父级 $loop-&gt;index,您必须使用 $loop-&gt;parent-&gt;index

对于您的语法错误,您可以执行以下操作:

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

【讨论】:

    猜你喜欢
    • 2017-06-20
    • 2018-04-04
    • 2021-10-19
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2015-12-15
    • 2017-11-13
    • 2017-10-11
    相关资源
    最近更新 更多