Home / Snippets / Laravel Where Not Equal To Code Example
Laravel Where Not Equal To Code Example cover

Laravel Where Not Equal To Code Example

652

3 years ago

0 comments

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!
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this