I have tables called event and eventinfo. Where the event hasMany eventinfo.
I am trying to get the event that has eventinfo with active=1, but it get all the eventinfo whether active is 0 or 1.
$eventsInfo = Event::find($eventId)
->whereHas('EventInfo', function (Builder $query) {
$query->where('active', 1);
});
can anyone point me where is the issue?
2
Answers
If you want to filter
EventInfo
that’s loaded, you should use thewith
function with a closure to apply conditions to the eager loaded models.In the code in question, just write
find()
afterwhereHas()
, so that you can get the Event with EventInfo active=1.P.S.
If it still doesn’t work, please use
toSql()
to print out the SQL query statement to check.