Laravel在数据透视表上搜索多个值

I'm trying to do a search on a belongs to many relationship.

I have links, and link tags, a link can have many link tags.

So what I'm trying to do is search for two tags, but I only want it to return links which have BOTH tags. So far I have this:

        $links = Link::whereHas('linktags', function($q) use($query, $where) {
            $q->whereIn('name', $query);
        })->with('linktags')->get();

This does search the relationship, and returns a result, but it matches ANY of the tags, not BOTH of them.

I've looked at the documentation but I cannot find a way of doing this. Any ideas?

I'm not exactly sure what you are putting in your $query and $where var, but according to the 4.2 docs you can just add simple "wheres" to your closure, like so:

$links = Link::whereHas('linktags', function($q) use($query, $where, $tag1, $tag2) {
    $q->where('name', $tag1);
    $q->where('name', $tag2);
})->with('linktags')->get();