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

TechKat's avatar

[HELP] Efficent way to get data from DB?

I have a table that's probably going to contain a lot of data, some of this data is categorized by type.

Say for example, 3 records is a type of Fruit, 10 records is a type of Vegetable, and 100 records is a type of Drink.

I want to seperate these types into their own HTML table, without having to call Modal::where('type', 'Fruit') for each of these tables.

Is there an efficent way to sort of so this using just one Modal class call?

So something along the lines of:

$consume = new Modal;
return View::make('consume')->withConsume($consume);

And consume.blade.php would do:

@foreach($consume->where('type', 'Vegetable') as $c)
@endforeach

I should probably point out that I am using Laravel 4.2 for this.

0 likes
4 replies
JarekTkaczyk's avatar
Level 53

@TechKat

$types =  ['Fruit', 'Vegetable', 'Drink'];
$consume = Modal::whereIn('type', $types)->get(); // or simply ::all() if you want all of them

$fruits = $consume->filter(function ($item) {
    return $item->type == 'Fruit';
}
// the same for other types, then pass these collections to your view
1 like

Please or to participate in this conversation.