显示类别和子类别(Laravel Blade)

时间:2018-07-12 16:14:06

标签: php html laravel laravel-blade

我在显示类别和子类别方面存在问题,我做到了 在我的刀刃中

       <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="{{ url('/') }}"><b class="fa fa-home"></b> Начало</a></li>
                @foreach ($categories as $cat)
                    @foreach($subcategories as $sub)

                        @if($sub->parent_id == $cat->id)
                            <li class="dropdown ">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                    {{$cat->name}} <i class="fa fa-caret-down"></i>
                                </a>
                                <ul class="dropdown-menu">
                                    <li><a href="{{ url('category/'.$sub->slug) }}">{{ $sub->name }}</a></li>
                                </ul>
                            </li>
                        @endif
                    @endforeach
                @endforeach
            </ul>
        </div>
    </div>
</nav>

它仅在具有一个子类别时显示类别,我不知道为什么发生!谁来帮帮我! (请不要选错票)

这是我的变量:

    $categories = Category::where('parent_id', NULL)->get();
    $subcategories = Category::where('parent_id','!=',NULL)->get();

2 个答案:

答案 0 :(得分:0)

这里的问题是,仅当存在子类别时,才在foreach循环中创建列表项。

此示例将为您解决问题。

<div class="collapse navbar-collapse">
    <ul class="nav navbar-nav">
        <li class="active"><a href="{{ url('/') }}"><b class="fa fa-home"></b> Начало</a></li>
        @foreach ($categories as $cat)
        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                {{$cat->name}} <i class="fa fa-caret-down"></i>
            </a>
            @foreach($subcategories as $sub)
                @if($sub->parent_id == $cat->id)
                    <ul class="dropdown-menu">
                        <li><a href="{{ url('category/'.$sub->slug) }}">{{ $sub->name }}</a></li>
                    </ul>
                @endif
            @endforeach
        </li>
        @endforeach
    </ul>
</div>

它未经测试,所以请告诉我它是否无效。

答案 1 :(得分:0)

我对其进行了很小的修改,但是现在可以工作了,谢谢@ answered26

这是我的改变

            <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="{{ url('/') }}"><b class="fa fa-home"></b> Начало</a></li>
                @foreach ($categories as $cat)
                    <li class="{{ $cat->children->count() > 0 ? 'dropdown' : ''}}">
                        <a href="#" class="{{ $cat->children->count() > 0 ? 'dropdown-toggle' : ''  }}" data-toggle="dropdown">
                            {{$cat->name}} {!! $cat->children->count() > 0 ? '<b class="fa fa-caret-down"></b>' : '' !!}
                        </a>
                        <ul class="dropdown-menu">
                        @foreach($subcategories as $sub)
                            @if($sub->parent_id == $cat->id)
                                    <li><a href="{{ url('category/'.$sub->slug) }}">{{ $sub->name }}</a></li>
                            @endif
                        @endforeach
                        </ul>
                     </li>
                @endforeach
            </ul>
        </div>
    </div>