我有两个表,类别和商品,每个类别都有ID和名称。每篇文章都有ID,名称和类别ID。现在,当用户单击特定的category_id时,我想在每个选项卡上加载数据。因此,相关产品将显示在每个类别选项卡上。我已经尝试过,但是无法解决。
类别模型
public function articles()
{
return $this->belongsToMany('App\Article')->withTimestamps();
}
商品模型
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
控制器
public function index(){
$categories = Category::with('articles')->whereIn('id', [1, 2, 3])->get();
return view('welcome',compact( 'categories'));
}
这是视图:
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
@foreach($categories as $category)
<a class="nav-item nav-link " id="nav-home-tab-{{$category->id}}" data-toggle="tab" href="#nav-home-{{$category->id}}" role="tab" aria-controls="nav-home" aria-selected="true">{{$category->title}}</a>
@endforeach
</div>
</nav>
@foreach($categories as $category)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home-{{$category->id}}" role="tabpanel" aria-labelledby="nav-home-tab-{{$category->id}}">
<div class="row clearfix">
@foreach($category->articles as $article)
<div class="col-md-3">
<div class="box mt-2 mb-2">
<img src="{{ $article->images ['thumb'] }}">
</div>
</div>
@endforeach
</div>
</div>
</div>
@endforeach
答案 0 :(得分:0)
$data["your key name"]