企业站三级分类(递归)

//控制器
public function getIndex(Request $request)
{
    $productCategoryList = ProductType::where('is_del',1)->get();
    $product_category_list =  $this->ProductCategoryRecursion($productCategoryList);

    return view('Front/Product/index',[
        'product_category_list'=>$product_category_list,
    ]);


}

// 分类递归
function ProductCategoryRecursion($list,$pid=0)
{
    $product_category = array();
    foreach ($list as $key => $value){
        if ($value->pid == $pid){
            $product_category[$key] = $value;
            $product_category[$key]['son'] = $this->ProductCategoryRecursion($list,$value->id);
        }
    }
    return $product_category;
}
//前台

<ul>
    @foreach($product_category_list as $key => $value)//一级栏目
        <li>
            <div class="fj">
                <span class="n_img">
                    <span></span>
                    {{--<img src="{{asset('after/images/nav1.png')}}" />--}}
                </span>
                <span class="fl">{{$value->categori_name}}</span>
            </div>
            <div class="zj">
                <div class="zj_l">
                    @foreach($product_category_list[$key]['son'] as $k =>$val)//二级栏目
                        <div class="zj_l_c">
                            <h2>{{$val->categori_name}}</h2>
                            @foreach($product_category_list[$key]['son'][$k]['son'] as $item)//三级栏目
                                <a href="#">{{$item->categori_name}}</a>|
                            @endforeach
                        </div>
                    @endforeach
            </div>
        </li>
    @endforeach
</ul>
//效果图

猜你喜欢

转载自blog.csdn.net/qq_39191303/article/details/80563415