Palzin Track
Get 15% off with code PTRACKSIGNUP15 

Laravel Diary Logo

Avoid Queries in Blade Templates & Use Eager Loading

tooling
Table of Contents

Bad (for 100 users, 101 DB queries will be executed):

@foreach (User::all() as $user)
{% raw %}   {{ $user->profile->name }}{% endraw %}
@endforeach

Good (for 100 users, 2 DB queries will be executed):

$users = User::with('profile')->get();

...

@foreach ($users as $user)
{% raw %}   {{ $user->profile->name }}{% endraw %}
@endforeach

::Share it on::

Comments (0)

What are your thoughts on "Avoid Queries in Blade Templates & Use Eager Loading"?

You need to create an account to comment on this post.

Related articles