【发布时间】:2015-09-29 22:00:30
【问题描述】:
我正在尝试使用 Laravel 5 eloquent 查询多对多关系,使用数据透视表,im trying to retrieve only "articles" that has tag "A" and tag "B" together, but not succeed, eloquents 带来了带有标签“A”和标签“B”的文章,有什么方法可以做到这一点?没有任何效果。
$tags = [1,2,3];
$result = Timetable::with([
'tags' => function($query) use($tags){
foreach($tags as $tag) {
$query->where('id', $tag);
}
}
])->paginate();
$result = Timetable::with(['tags'])
->whereHas('tags' => function($query) use($tags){
$query->whereIn('tag_id',$tags);
}
)->paginate();
【问题讨论】: