mardi 31 mai 2016

Laravel 5 Eloquent - whereIn on join table

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:

"Indian",

"Chinese"

etc..

I have 3 tables:

Business

Tags

Business Tags

And my model is as follows:

class Business extends Model
{
    protected $table = 'businesses';

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

    public function tags()
    {
        return $this->belongsToMany('App\Tags');
    }
}

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

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

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

})->get();

The issue is that it takes forever. I don't even think this is the best way to do such a thing. Anyone help me with a better solution that doesn't take forever to load the results?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire