我有以下多对多关系:
attendees - attendee_tag - tag
现在我想根据一些标记值过滤与会者,所以我创建了这个解决方案,效果很好:
$attendees = Attendee::whereHas('tags', function ($q) {
$q->where([
['attendee_tag.tag_id', '=', '6'],
['attendee_tag.value_string', '=', 'Summer ticket']
})->with(['tags' => function($q) {
$q->select('tags.id', 'value_type', 'attendee_tag.value_string', 'attendee_tag.value_int');
}])->paginate($pagination);
但我无法弄清楚,如何根据超过1个标签过滤与会者...我已经尝试过这个,但它没有用:
$attendees = Attendee::whereHas('tags', function ($q) {
$q->where([
['attendee_tag.tag_id', '=', '6'],
['attendee_tag.value_string', '=', 'Summer ticket']
])->where([
['attendee_tag.tag_id', '=', '13'],
['attendee_tag.value_string', '=', 5]
]);
})->with(['tags' => function($q) {
$q->select('tags.id', 'value_type', 'attendee_tag.value_string', 'attendee_tag.value_int');
}])->paginate($pagination);
任何帮助都将受到高度赞赏! :)
答案 0 :(得分:0)
也许分开whereHas:
$attendees = Attendee::whereHas('tags', function ($q0) {
$q0->where([
['attendee_tag.tag_id', '=', '6'],
['attendee_tag.value_string', '=', 'Summer ticket']
]);
})->whereHas('tags', function ($q1) {
$q1->where([
['attendee_tag.tag_id', '=', '13'],
['attendee_tag.value_string', '=', 5]
]);
})->with(['tags' => function($q) {
$q->select('tags.id', 'value_type', 'attendee_tag.value_string', 'attendee_tag.value_int');
}])->paginate($pagination);