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

ctyler's avatar

Unable to use WhereNotIN with where and/ orWhere

Hello Everyone, I have a class that has enrollment. I have a field that is populated with ajax. It is one field that uses a LIKE condition on three columns or my users table. It work perfectly when I use

$data = DB::table('users')
                ->select('users.id', 'users.firstname', 'users.lastname', 'users.email')
                ->where('firstname', 'LIKE', "%{$query}%")
                ->orwhere('lastname', 'LIKE', "%{$query}%")
                ->orwhere('email', 'LIKE', "%{$query}%")
                ->orderBy('lastname')
                ->take(25)
                ->get();

the problem is, I do not want users already enrolled to show up as options. So if I do something like this

            $data = DB::table('users')
                ->select('users.id', 'users.firstname', 'users.lastname', 'users.email')
                ->whereNotIn('users.id', $usersEnrolled)
                ->where('firstname', 'LIKE', "%{$query}%")
                ->orwhere('lastname', 'LIKE', "%{$query}%")
                ->orwhere('email', 'LIKE', "%{$query}%")
                ->orderBy('lastname')
                ->take(25)
                ->get();

I am getting enrolled users from

$usersEnrolled = $enrollment->users()->pluck('id');

Anyone have any idea how I can get this to work?

0 likes
2 replies
MichalOravec's avatar
Level 75
$data = DB::table('users')
    ->select('users.id', 'users.firstname', 'users.lastname', 'users.email')
    ->whereNotIn('users.id', $usersEnrolled)
    ->where(function($query) use ($query) {
        $query->where('firstname', 'LIKE', "%{$query}%")
            ->orwhere('lastname', 'LIKE', "%{$query}%")
            ->orwhere('email', 'LIKE', "%{$query}%")
    })
    ->orderBy('lastname')
    ->take(25)
    ->get();

You should always group orWhere calls in order to avoid unexpected behavior when global scopes are applied.

https://laravel.com/docs/8.x/queries#or-where-clauses

1 like
ctyler's avatar

OMG. @michaloravec thank you so much I appreciate your time. I have such a hard time with precedence and grouping. Your the best!

Please or to participate in this conversation.