Posts Learn Components Snippets Categories Tags Tools 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

201 views

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

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.
<?php

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.
<?php

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 ☕️

Load comments for Laravel Where Not Equal To Code Example

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components
)