我正在尝试根据子类别显示相关产品。在此菜单中,当我单击父类别时,它会显示产品,但单击子类别产品时未显示,我想在单击子类别时显示产品。
有人有任何想法吗?
在此先感谢您的帮助 :)
类别表
]
产品表
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');
}
Controller
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 View
<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');
最佳答案
类别模型
public function category()
{
return $this->hasMany('App\category','p_id','id');
}
Controller
public function procateg(Request $request){
$cate_name=$request->cate_name;
$product = DB::table('category')
->Join('products','category.id','=','products.cate_id')
->where('category.cate_name','=',$cate_name)->get();
return view('front_end/products',compact('product'));
}
关于laravel - 我想使用 laravel 根据子类别显示相关产品,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59530259/