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

untymage's avatar

Shorter version of this query

$query->whereNotIn('id',
   Animal::where([category' => 'animal', 'type' => 'dog'])
       ->orWhere(['category' => 'animal', 'type'=>'bear'])->pluck('animal_id')->all()
)->orWhereIn('id', Animal::where(
    ['category' => 'animal', 'type' => 'cat']
)->pluck('animal_id')->all())
    ->get();

Say i have House and Animal model and having five type of animal 'Cat','Bear','Dog','Eagle','Scorpion' in House Model i want to grab only cat and rest except dog and bear (in feature i will add more), This query works but i think it so long.

0 likes
9 replies
Snapey's avatar

your ->all() statements are redundant

1 like
Snapey's avatar

i want to grab only cat and rest except dog and bear

sorry but this makes no sense...

untymage's avatar

@snapey Ok , Is there any method that i can reduce this two where clause:

Animal::where([category' => 'animal', 'type' => 'dog'])
       ->orWhere(['category' => 'animal', 'type'=>'bear'])

looking for something like this :

Animal::where([category' => 'animal', 'type' =>[ 'dog','bear'])
       
Snapey's avatar
Snapey
Best Answer
Level 122
Animal::where('category', 'animal')
    ->wherein('type' ,[ 'dog','bear'])
    ->get()

1 like
untymage's avatar

Thanks guys i feel better right now <3

Snapey's avatar

please mark it answered (if it is)

untymage's avatar

removing ->all() and adding wherein helped, But these are in separate replies 😆 sorry but these really helped me, i appreciated.

Snapey's avatar

removing all() was irrelevant in the end since the whole query was different

Please or to participate in this conversation.