WhereNotIn in WhereHas not working #1807
-
I have a Song model and I'm trying to filter out songs by category tags, in both a white and blacklist. const requestTags = [3];
const requestNotTags = [2, 4];
const resultQuery = Song.query()
.select('id', 'title', 'duration', 'type')
.preload('categories', (query) => {
query.select('name');
})
.whereHas('categories', (query) => {
if (requestTags.length > 0) {
query.whereIn('categories.id', requestTags); // This works perfectly fine
}
if (requestNotTags.length > 0) {
query.whereNotIn('categories.id', requestNotTags); // This doesn't work
}
}); I initially thought this was a bug when I asked the question, but now understand it's obviously not. Anyway, there's more discussion about here with details. Also, this near identical scenario was asked in Laravel Eloquent, but the answer didn't seem to work for me. Does anyone have any clue to get the desired result with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You have to be more explicit when you say "This doesn't work"? Also, you have to narrow down where the issue is. If there is a bug in the query builder, then it will produce the wrong SQL query. So first thing is to debug and check the SQL query. |
Beta Was this translation helpful? Give feedback.
You have to be more explicit when you say "This doesn't work"? Also, you have to narrow down where the issue is.
If there is a bug in the query builder, then it will produce the wrong SQL query. So first thing is to debug and check the SQL query.