Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Uladzimir's avatar

Why WhereNotIn don't work in opposite?

Want to exclude films, what contain certain tags use function like

return $builder->whereHas('tags', function ($query) use ($value) {
            $query->whereIntegerNotInRaw('id', $value);

But it worked in interesting way, it exclude just 184 films whichever tag i choose.

If we do vice versa and change whereIntegerNotInRaw to whereIntegerInRaw, there are all show correctly. It shows movies that contain the selected tags.

BUT in my case I want to do that it exclude films with choisen tags. How to do it correctly?

0 likes
4 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Try reversed

return $builder->whereDoesntHave('tags', function ($query) use ($value) {
            $query->whereIntegerInRaw('id', $value); 
1 like
Uladzimir's avatar

@Sinnbeck "whereDoesntHave" its super and it works, I don't sow it and now see in documentation of laravel. Thank you wery much!

Please or to participate in this conversation.