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

untymage's avatar

return two or more result from pivot table

What is the shorten version of this :

$this->models->filter(function ($model) {
    return $model->pivot->pivotcolumn == 'one' or 
    $model->pivot->pivotcolumn == 'two';
})->values();

it will work but what if i want filter through over three or more pivot table ? When i using return $model->pivot->pivotcolumn == 'one' or 'two' it return all result and wouldnt filter it.

0 likes
2 replies
mstrauss's avatar
mstrauss
Best Answer
Level 14

How about ->wherePivotIn?

$this->models->filter(function ($model) {
    return $model->wherePivotIn('pivotcolumn', ['one', 'two', 'three'])
})->values();

Please or to participate in this conversation.