People searching “Laravel N+1 Vue” open a list of posts and the debug bar shows one query per author. Send the relation with the page. The API resource guide is the JSON shape. An Inertia list can pass the columns directly.
TL;DR
- with(‘author’) loads the relation once.
- Reading it inside a loop is one query per row.
- Do not call
$post->authorinside amap()that builds the prop after you forgotwith(). - Do not eager load every relation on the model “so the page has it”.
The fix#
$posts = Post::query()
->with('author:id,name')
->orderBy('id')
->paginate(12);author:id,name limits the columns. The foreign key has to be in the select or the relation cannot match, so leave id on the user.
<li v-for="post in posts.data" :key="post.id">
{{ post.title }} <span v-if="post.author">{{ post.author.name }}</span>
</li>What to avoid#
Gotcha
Do not call $post->author inside a map() that builds the prop after you forgot with(). That is the N+1. Do not eager load every relation on the model “so the page has it”. The HTML includes what you load.
FAQ#
How do I eager load a relation for a Vue list?
with('author') loads the relation once. Reading it inside a loop is one query per row.
What goes wrong with Laravel N+1 Vue?
Many developers open a list of posts and the debug bar shows one query per author. Do not call $post->author inside a map() that builds the prop after you forgot with().



