Can I simplify this code without foreach?
$userQuestIds = [2,4,45,586,16,2,143,234,654,78,56];
$typeQuests = [];
foreach ($userQuestIds as $qId) {
$typeQuests[] = Quest::where(['id' => $qId])->first();
}
Can I simplify this code without foreach?
$userQuestIds = [2,4,45,586,16,2,143,234,654,78,56];
$typeQuests = [];
foreach ($userQuestIds as $qId) {
$typeQuests[] = Quest::where(['id' => $qId])->first();
}
3
Answers
You can use
whereIn
:NOTE: this approach is better for columns other than
id
(as primary). I think @andriy-Lozynskiy solution is the best way.If
id
is a primary key the shortest way is:You can use the
whereIn()
method on theQueryBuilder
class