Are those all the possible types or are there more?
You can do this if it isn't
$items = Item::whereIn('type', ['Meat', 'Premium', 'Drink', 'Soda', 'Side'])->get();
//and
$meats = $items->where('type', 'Meats');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I previously wrote some code but now that I'm looking at it about a year I feel like there has to be a more optimized way of getting the same result.
I have a Model for Items, the Items have a 'type' attribute. This is how I am currently getting the Items by type, using Item::where('type','foo')->get(); In total this is running the same query 5 times.
$meats = Item::where('type', 'Meat')->get();
$premiums = Item::where('type', 'Premium')->get();
$drinks = Item::where('type', 'Drink')->get();
$sodas = Item::where('type', 'Soda')->get();
$sides = Item::where('type', 'Side')->get();
I came up with this solution yesterday but I feel like it's kind of ugly. Both of the solutions work but my question is, what is the proper way of doing it?
$items = Item::all();
foreach($items as $item){
if($item->type == 'Meat'){
$meats[] = $item;
}
if($item->type == 'Premium'){
$premiums[] = $item;
}
if($item->type == 'Drink'){
$drinks[] = $item;
}
if($item->type == 'Soda'){
$sodas[] = $item;
}
if($item->type == 'Side'){
$sides[] = $item;
}
}
Are those all the possible types or are there more?
You can do this if it isn't
$items = Item::whereIn('type', ['Meat', 'Premium', 'Drink', 'Soda', 'Side'])->get();
//and
$meats = $items->where('type', 'Meats');
Please or to participate in this conversation.