普通网友 2015-07-02 09:05
浏览 18

类别没有显示出所需,有什么问题?

I have categories like this:

Categories Image

I want to create a drop down menu for the categories, which I have successfully created. But the problem is that the child category is also shown as parent category whenever I click on the link.

See the below image:

enter image description here

I want Western category should appear only when Apparels category is clicked (which works perfectly fine) and it should not appear when clicked on the Categories link.

Category Model:

protected $fillable = [
        'name', 'parent_id'
];

public function products()
{
    return $this->belongsToMany('App\Product')->withTimestamps();
}

public function childs()
{
    return $this->hasMany('App\Category', 'parent_id', 'id');
}

In Contoller:

$category = Category::where('parent_id', '!=', '0')->with('childs')->get();

The view:

<li class="dropdown">
    <a href="javascript:void(0)" class="dropdown-toggle links-titilium" data-toggle="dropdown" role="button" aria-expanded="false">Categories <span class="caret"></span></a>
    <ul class="dropdown-menu multi-level" role="menu">
        @foreach( $category as $cat )
        <li @if($cat->childs->count()) class="dropdown-submenu" @endif>
        <a class="links-titilium" href="{{ url( '/store/category', [$cat->id, Safeurl::make($cat->name)] ) }}" @if( $cat->childs->count() ) class="dropdown-toggle" data-toggle="dropdown" @endif>
        {{ $cat->name }}
        </a>
        @if( $cat->childs->count() )
        <ul class="dropdown-menu" role="menu">
            <li>
                @foreach( $cat->childs as $child )
                <a href="{{url('/category', [Safeurl::make($cat->name), Safeurl::make($child->name)])}}">
                {{ $child->name }}
                </a>
                @endforeach
            </li>
        </ul>
        @endif
        </li>
        @endforeach
    </ul>
</li>
  • 写回答

2条回答 默认 最新

  • duanqian9593 2015-07-02 09:28
    关注
    $category = Category::where('parent_id', '!=', '0')->with('childs')->get();
    

    as per your table rows above line will fetch all the categories. To fetch the categories with level 1 try the below line.

    $category = Category::where('parent_id', '1')->with('childs')->get();
    
    - Apparels
    - Clothes
      -- Western
    

    or to fetch the top level categories with child categories

    $category = Category::where('parent_id', null)->with('childs')->get();
    
    - None
      - Apparels
      - Clothes
        -- Western
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行