如何在Laravel中显示细分中的类别

时间:2019-07-13 17:51:00

标签: laravel laravel-5

我希望能够显示不同div中范围内的类别,例如

(div 1)Cat 1,Cat 2,Cat 3,Cat 4(/ div)
(div 2)Cat 5,Cat 6,Cat 7,Cat 8(/ div)

下面是我所拥有的。如您所见,它基本上提取所有类别及其子类别

(div 1)Cat 1,Cat 2,Cat 3,Cat 4,Cat 5,Cat 6,Cat 7,Cat 8,Cat 8(/ div)

      @foreach ($categories as $key => $cols)
    @foreach ($cols as $iCat)
            <div class="maincategories">
             <?php $attr = ['countryCode' => config('country.icode'), 'catSlug' => $iCat->slug]; ?>
                <?php $attr = ['countryCode' => config('country.icode'), 'catSlug' => $iCat->slug]; ?>
                <div class="maincategories-list clr">
                    <div class="li fleft">
                        <div class="item">
                            <a href="{{ lurl(trans('routes.v-search-cat', $attr), $attr) }}" data-id="{{ $iCat->id }}" class="link parent   ">
                                <span>{{ $iCat->name }}</span>
                                <span class="cat-icon cat-icon-circle cat-icon-{{ $iCat->id }}"></span>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="subcategories-list clr" style="display:none" data-subcategory="{{ $iCat->id }}" id="bottom{{ $iCat->id }}">
                    <div class="subcategories-title">
                        <span class="courierbadge">
                            <span class="ctt-delivery-icon"></span>
                        </span>
                        <a href="{{ lurl(trans('routes.v-search-cat', $attr), $attr) }}" data-id="{{ $iCat->id }}" class="link inlblk"><strong>See all ads</strong></a> in {{ $iCat->name }} </div>
                    <ul>
                        @if (isset($subCategories) and $subCategories->has($iCat->tid))
                        @foreach ($subCategories->get($iCat->tid) as $iSubCat)
                        <li class="fleft">
                            <?php $attr = ['countryCode' => config('country.icode'), 'catSlug' => $iCat->slug, 'subCatSlug' => $iSubCat->slug]; ?>
                            <a data-id="293" data-category-id="{{ $iCat->name }}" href="{{ lurl(trans('routes.v-search-subCat', $attr), $attr) }}" class="link-relatedcategory cat-{{ $iCat->name }} inlblk tdnone icon-link">
                                <span class="link block category-name">
                                            <span>{{ $iSubCat->name }}</span>
                                </span>
                            </a>
                        </li>
                         @endforeach
                    @endif                            
                    </ul>
                </div>
            </div>
            <input type="hidden" id="category-id-hidden" name="category_id" value="" />
            @endforeach
        @endforeach

1 个答案:

答案 0 :(得分:0)

Laravel提供了一个特殊的$loop变量(Documentation),您可以利用它来发挥自己的优势:

<div> <!-- The div that will contain 4 elements -->
    @foreach (/* your loop */)
        <div>A category</div>

        @if ($loop->index % 4 == 3 && !$loop->last)
            </div>
            <div>
        @endif
    @endforeach
</div>

$loop->index表示当前循环迭代。它从0开始。% 4表示“模4”,它是当前迭代除以4的余数。这表示$loop->index % 4将为0、1、2、3、0, 1、2、3、0、1、2、3等

通过检查是否等于3,它正在关闭div并打开另一个。我添加了一个额外的检查!$loop->last,以便在恰好有4个元素(或8、12,...)的情况下,再次不关闭+再次打开div,因为最后一个元素%4为3。 / p>