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
No, where clause is from Collection class
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.
Thank you Sergiu17, I know jlrdw, it is just a sample.
It would be better to do all queries in your controller and pass the data to the views and iterate from there, using @foreach
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
Thank you all for your advices.
Please sign in or create an account to participate in this conversation.