More with where in (list_of_items)
:
$linkIds = $user->links()->pluck('id')->toArray();
$tags = Tag::query()
->join('link_tag', function (JoinClause $join) use ($linkIds) {
$joinClause = $join->on('tags.id', '=', 'link_tag.tag_id');
$joinClause->on('link_tag.link_id', 'in', $linkIds ?: [-1], 'and', true);
})
->groupBy('link_tag.tag_id')
->get();
return $tags;
Hope it helpful ;)