
Avoid Queries in Blade Templates & Use Eager Loading
tooling
Table of Contents
Do not execute queries in Blade templates and use eager loading (N + 1 problem)
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
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.