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

Armani's avatar
Level 17

useing Eloquent in blade file make another query?

In controller I got all users with this:

$users = User::all();

return view('users, compact('users'));

if I use this code in blade, does this make another query?

@foreach($users->where('active', true) as $user)
	<li>$user->name</li>
@endforeach
0 likes
6 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

No, where clause is from Collection class

jlrdw's avatar

Do your where in the controller or even a model query scope, then pass it to The View and foreach.

In other words narrow your results prior to passing to view.

Armani's avatar
Level 17

Thank you Sergiu17, I know jlrdw, it is just a sample.

ComputerMaverick's avatar

It would be better to do all queries in your controller and pass the data to the views and iterate from there, using @foreach

shez1983's avatar

you got your answer, no it doesnt.. but try to follow their advice and narrow down your query in controller otherwise you are wasting precious processing io/user will see delay etc

Armani's avatar
Level 17

Thank you all for your advices.

Please or to participate in this conversation.