Level 102
You filter with() as well
$venues = Venue::whereHas('event', function (Builder $query) {
$query->where('category_id', '2');
})->with(['event' => function ($query) {
$query->where('category_id', '2');
}])->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I have venue's with events. The events have a category. I want to show a page with all venues with their events. But I only want to show the veneus with the category with id '2'. Code below shows the venues containing an event with category '2'.
How can I show only the events that are category '2' because now I see all events.
$venues = Venue::whereHas('event', function (Builder $query) {
$query->where('category_id', '2');
})->get();
Thanks!
You filter with() as well
$venues = Venue::whereHas('event', function (Builder $query) {
$query->where('category_id', '2');
})->with(['event' => function ($query) {
$query->where('category_id', '2');
}])->get();
Please or to participate in this conversation.