Level 50
$departments = Departments::whereIn('id', [1,2,3,4,5,6,7]);
https://laravel.com/docs/master/collections#method-wherein
Happy coding!
2 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
$departments = [];
foreach ([1, 2, 3, 4, 5, 6, 7] as $department) {
array_push($departments, Department::find($department));
}
return $departments;
How can I make all these multiple query on a single database connection for better performance?
find function is able to receive an array in the $id argument:
$departments = Department::find([1, 2, 3, 4, 5, 6, 7])
Please or to participate in this conversation.