我想使用laravel显示基于子类别的相关产品

时间:2019-12-30 11:41:06

标签: laravel

我正试图显示基于子类别的相关产品。在此菜单中,当我单击父类别时,它将显示产品,但是在未显示子类别产品的单击时,我想在单击子类别时显示产品。

有人有什么主意吗?

在此先感谢您的帮助:)

类别表

enter image description here] 产品表

product has  id product_name ,product_image,price,cate_id 

视频

请观看视频以更好地理解谢谢
https://www.loom.com/share/5b91deedac154c98bc275c9edcd3b773

类别模型

public function category()
{
return $this->hasMany('App\category','p_id');
 }

控制器

public function procateg(Request $request){
 $cate_name=$request->cate_name;       
 $product=DB::table('category')
 ->leftjoin('products','category.id','=','products.cate_id')  
 ->where('category.cate_name','=',$cate_name)->paginate('8');    
  return view('front_end/products',compact('product'));   
 }

HTML视图

<div class="nav-panel__nav-links nav-links">
    <ul class="nav-links__list">
        @foreach(App\category::with('category')->where('p_id',0)->get() as $firstmenu)
            <li class="nav-links__item  nav-links__item--has-submenu ">
                <a id="cate" class="nav-links__item-link" href="{{url('/product/'.$firstmenu- 
        >cate_name)}}">
                    <div class="nav-links__item-body">
                        {{$firstmenu->cate_name}}
                        <svg class="nav-links__item-arrow" width="9px" height="6px">
                            <use xlink:href="{{url('public/assets/images/sprite.svg#arrow-rounded-down-9x6')}}">
                            </use>
                        </svg>
                    </div>
                </a>
                <div class="nav-links__submenu nav-links__submenu--type--megamenu nav-links__submenu-- 
         size--nl">
                    <!-- .megamenu -->
                    <div class="megamenu ">
                        <div class="megamenu__body">
                            <div class="row">
                                @foreach($firstmenu->category as $secondmenu)
                                    <div class="col-6">
                                        <ul class="megamenu__links megamenu__links--level--0">
                                            <li class="megamenu__item  megamenu__item--with-submenu ">
                                                <a href="{{url('product'.'/'.$firstmenu->cate_name.'/'.$secondmenu->cate_name)}}">
                                                    {{$secondmenu->cate_name}}</a>
                                                <ul class="megamenu__links megamenu__links--level--1">
                                                    @foreach($secondmenu->category as $thirdmenu)
                                                        <li class="megamenu__item"><a href="{{url('product'.'/'.$firstmenu- 
          >cate_name.'/'.$secondmenu->cate_name.'/'.$thirdmenu->cate_name)}}">
                                                                {{$thirdmenu->cate_name}}</a>
                                                        </li>
                                                    @endforeach
                                                </ul>
                                            </li>
                                        </ul>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                    <!-- .megamenu / end -->
                </div>
            </li>
        @endforeach
    </ul>
</div>

路线

 Route::get('product','FrontController@product');
 Route::get('product/{cate_name}','FrontController@procateg');

1 个答案:

答案 0 :(得分:1)

类别模型

SELECT OJ.value
FROM (VALUES(@Error_Description))V(Error_Description)
     CROSS APPLY (VALUES('["' + REPLACE(REPLACE(REPLACE(V.Error_Description,'"','","'),NCHAR(13),''),NCHAR(10),'')+ '"]'))R(JSON)
     CROSS APPLY OPENJSON(R.JSON) OJ
WHERE OJ.[Key] % 2 = 1;

控制器

public function category()
{
   return $this->hasMany('App\category','p_id','id');
}