You can use the package Laravel Debugbar during development and check out your queries that are being executed for each HTTP request.
how do I query this relation with eloquent
I'm using Laravel and eloquent obviously. I have 4 models and tables.
1 => city
2 => location
3 => venue
4 => tag
now bear with me to describe relationship. a city has many locations and a location belongs to a city. a location belongs to a venue and a venue has one location. a venue belongs to many tags and a tag belongs to many venue (Many to many relationship)
Here are the query I wanna executed. given A city, how can I filter venues of that city based on tags. example: for city Austin I wanna get venues that have tag "special".
and Also, how many queries are executed for perform this task.Is it efficient to perform this kind of task with this database model.
I tried to be as explicit as possible but if some parts seems vague for you, please don't hesitate to ask. thanks
Your relations should be:
City hasMany Location
Location hasOne Venue (or hasMany maybe?)
City hasManyThrough Venue, Location
then as simple as this:
$city = City::where('name', 'Austin')->firstOrFail();
// having one tag, assuming tags.name is the field for lookup
$tag = 'special';
$city->venues()->whereHas('tags', function ($q) use ($tag) {
$q->where('name', $tag);
})->get();
// having any of these tags
$tags = ['special', 'not-that-special'];
$city->venues()->whereHas('tags', function ($q) use ($tags) {
$q->whereIn('name', $tags);
})->get();
// having all of these tags, assuming there are no duplicates venue-tag in pivot table
$tags = ['special', 'not-that-special'];
$city->venues()->whereHas('tags', function ($q) use ($tags) {
$q->whereIn('name', $tags);
}, '=', count($tags))->get();
Please or to participate in this conversation.