Laravel 5 Eloquent - whereIn on join table
<p>I am trying to create an online directory, where, for example, people can search through the website and find all takeaways that have a specific type. For example:</p>

<p>"Indian",</p>

<p>"Chinese"</p>

<p>etc..</p>

<p>I have 3 tables:</p>

<p>Business </p>

<p>Tags </p>

<p>Business Tags </p>

<p>And my model is as follows:</p>

<pre><code>class Business extends Model
{
    protected $table = 'businesses';

    protected $fillable = [
        'business_name', 'postcode'
    ];

    public function tags()
    {
        return $this-&gt;belongsToMany('App\Tags');
    }
}
</code></pre>

<p>The issue is, whenever I come to do the search, and try to do a <code>whereIn</code> the issue is that it takes forever to load, in fact, it doesn't even load. For example:</p>

<pre><code>$business = Business::whereHas('tags', function($tag) use ($request) {

    if($request-&gt;get('terms'))
    {
        $tag-&gt;whereIn('tags.name', ['chinese']);
    }

})-&gt;get();
</code></pre>

<p>So my question is this: </p>

<p>I have just over 10k rows of data stored inside the table. This table is split into three "Business", "Tags", "Business Tags". The process above is taking so long to complete, probably because I use the <code>whereHas('tags')</code> and <code>whereIn</code> therefore, how do I go about using the following syntax:</p>

<pre><code>$business = Business::where( function ($business) use ($request) {

     // Search for businesses with a specific tag, passed from request

}); 
</code></pre>

<p>Is this possible?</p>