Yes that is normal. with() runs a query for each thing it needs to add. If you want joins, you need to write them yourself (but then you dont have relationships)
Feb 9, 2022
5
Level 2
->with() do not join but do another query
Hello,
My Debugbar displayed a lot of duplicated queries. So I tried to investigate. I would like to retrieve all the documents belonging to a user as well as all the authors of these documents (with their avatar). Here is the code :
$documents = $user->documents()->with('user.avatar')->get()
I thought that it would use "INNER JOIN" to select all that elements in 1 query. But it run 3 queries...
select * from `documents` where `is_visible` = ? and `documents`.`id` in (?, ?)
select * from `users` where `users`.`id` in (25)
select * from `avatars` where `avatars`.`id` in (8)
Is it a normal behavior ?
Thanks for your help.
Level 102
Please or to participate in this conversation.