Thread::with('tags')->get(); will load all threads and all tags with all pivot tables in two or three quires
Does eager loading only needed for one-to-many relationships?
Excuse me, When should i use eager loading on my relationships ? I have some many to many relationship but it doesnt affect the number of queries, For example, Tags has many to many (polymorphic) to another models, But no matter i eager load ($with = ['relation']) the relation the number of queries is big, my codes: https://laracasts.com/discuss/channels/laravel/n1-when-using-wherepivot
So the purpose of eager loading for only one to many relation for reducing queries ?
If you are currently viewing a thread. Then it makes no difference if you eager load or not, because the tags relation is only being called once.
If you are looping through the threads, and showing each tag like this:
@foreach($threads as $thread)
{{ $thread->name }}
@foreach($thread->tags as $tag)
{{ $tag->name }}
@endforeach
@endforeach
Then you would want to eager load, otherwise it will load the tag relation on each iteration of the loop.
Please or to participate in this conversation.