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

kramsuiluj's avatar

How to query whereIn AND whereIn AND whereIn in Query Builder?

I want to do this query in the query builder.

select * from users where 'value' in (firstname) and 'value' in (middlename) and 'value' in (lastname)

Right now my code looks like this:

$users = DB::table('users')
            ->whereIn('firstname', [$teacher->firstname])

How do I implement AND on this query?

0 likes
8 replies
MichalOravec's avatar
Level 75

Like this

->whereIn('firstname', [$teacher->firstname])
->whereIn('middlename', [$teacher->middlename])
->whereIn('lastname', [$teacher->lastname])

But it doesn't make any sense.

What are you trying to do?

kramsuiluj's avatar

@MichalOravec Oh so I just chain it like that, I see. Uh I had a set of unique value on my migration

$table->unique(['firstname', 'middlename', 'lastname']);

What I wanted to do with it is when a user input a set of value that is already in the database then return an error, to be honest, I don't really know if it would work but yeah its what I'm trying to do.

kramsuiluj's avatar

@MichalOravec Ok so I was really eyeing on that validation rule a while ago, but I can't seem to wrap my head around it, I can't form the logic for that approach.

'email' => Rule::unique('users')->where(function ($query) {
    return $query->where('account_id', 1);
})

I don't really get the function part inside the first where clause, please bear with me for a bit I will try to understand this approach and hopefully get my answer.

Does it simply translate to

'email' => Rule::unique('users')->where('account_id', 1)
kramsuiluj's avatar

@MichalOravec Thanks I got it, I did it like this.

Rule::unique('users')
                    ->whereIn('firstname', [request('firstname')])
                    ->whereIn('middlename', [request('middlename')])
                    ->whereIn('lastname', [request('lastname')])
1 like
kramsuiluj's avatar

@MichalOravec Oh sorry, I'll change it. I got it because before I wanted to do it in query builder, then I did some searching on how to do it in SQL and the query was using "where values in (column)", then I converted it to query builder that's how I got the whereIn

Please or to participate in this conversation.