Posts Learn Components Snippets Categories Tags Tools new About

Laravel Where Not Equal To Code Example

Learn how you can write your eloquent to query a condition where it's not equivalent to the easy way

Created on Nov 14, 2021


You can easily query a condition where it's not equivalent to using the "whereNotIn" method available to every eloquent instance.

Using whereNotIn() Method

For example, if you want to query all Posts where the ID is not 10, 11, 12, and 13 you can write your code as follows.

Post::query()->whereNotIn('id', [10, 11, 12, 13])->get();

Using where Method

Or an alternative to that is to use the normal "where" clause and then have the condition as follows.

Post::query()->where('id', '!=' , 10)->orWhere('id', '!=' , 11)->orWhere('id', '!=' , 12)->orWhere('id', '!=' , 13)->get();
Hope it helps and cheers, happy coding!

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️