title: Nested comment system runs too many queries ( laravel & livewire )
body: <p>I've built a nested comment system and it works fine, but I have a problem.
  when I want to show the comments, it runs too many queries. for example if I have 10 comments and two replies each, laravel debugger shows 40+ queries, and I want to reduce them, here is my code :</p>
  <p><strong>Query</strong></p>
  <pre><code>$comments = $post-&gt;mentions()-&gt;with(['author', 'reply'])-&gt;get();
  </code></pre>
  <p><strong>Relationship in Post.php Model</strong></p>
  <pre><code>public function mentions()
      {
          return $this-&gt;hasMany(Comment::class, 'post_id')-&gt;where('approved',1)-&gt;where('parent_id',0);
      }
  </code></pre>
  <p><strong>Comments Relationships</strong></p>
  <pre><code> public function author()
      {
          return $this-&gt;belongsTo(User::class, 'author_id')-&gt;select('id', 'name', 'avatar', 'role');
      }
  
      public function reply()
      {
          return $this-&gt;hasMany(Comment::class, 'parent_id')-&gt;where('approved', 1);
      }
  </code></pre>
  <p><strong>Blade File</strong></p>
  <pre><code>@foreach ($comments as $comment)
      &lt;div x-data=&quot;{reply:false}&quot; class=&quot;break-words bg-white border-2 rounded-lg p-4 my-5&quot; id=&quot;answer-{1}&quot;&gt;
          &lt;div wire:ignore class=&quot;flex justify-between items-center&quot;&gt;
              &lt;div class=&quot;flex items-center&quot;&gt;
                  &lt;img class=&quot;rounded-full w-20 border-4 @if ($comment-&gt;author-&gt;role == 'administrator') border-blue-500 @else border-gray-500 @endif &quot; src=&quot;{{ $comment-&gt;author-&gt;avatar }}&quot;
                      alt=&quot;{{ $comment-&gt;author-&gt;name }}&quot;&gt;
                  &lt;div&gt;
                      &lt;div class=&quot;flex pr-3&quot;&gt;
                          &lt;a href=&quot;/@username&quot;
                              class=&quot;font-bold text-lg text-gray-800 hover:text-gray-900&quot;&gt;{{ $comment-&gt;author-&gt;name }}&lt;/a&gt;
                          @if ($comment-&gt;author-&gt;role == 'administrator')
                              &lt;img class=&quot;w-5 mr-1&quot; src=&quot;/img/verified.svg&quot; alt=&quot;&quot; title=&quot;admin&quot;&gt;
                          @endif
                      &lt;/div&gt;
                      &lt;p class=&quot;font-light cursor-default text-gray-700 pt-1 pr-3&quot;&gt;
                          {{ $comment-&gt;created_at-&gt;diffForHumans() }}&lt;/p&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;svg @click=&quot;reply = true&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; class=&quot;h-6 cursor-pointer hover:text-rose-600&quot;
                  fill=&quot;none&quot; viewBox=&quot;0 0 24 24&quot; stroke=&quot;currentColor&quot;&gt;
                  &lt;path stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; stroke-width=&quot;2&quot;
                      d=&quot;M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6&quot; /&gt;
              &lt;/svg&gt;
          &lt;/div&gt;
          &lt;div class=&quot;comment_body leading-8 pt-3 text-gray-800 cursor-default&quot;&gt;
              &lt;div wire:ignore&gt;
                  {!! nl2br($comment-&gt;body) !!}
              &lt;/div&gt;
              &lt;div x-cloak x-show=&quot;reply&quot;&gt;
                  &lt;form class=&quot;py-3&quot; wire:submit.prevent=&quot;SubmitComment({{ $comment-&gt;id }})&quot;&gt;
                      &lt;textarea wire:model.defer=&quot;commentBody&quot;
                          class=&quot;w-full focus:ring-0 bg-zinc-200 border shadow focus:bg-zinc-100 mt-3 p-4 rounded-lg&quot;
                          id=&quot;editor&quot; rows=&quot;6&quot; placeholder=&quot;&quot;&gt;&lt;/textarea&gt;
                      @error('commentBody') &lt;p class=&quot;text-red-600 mt-1 text-sm&quot;&gt;{{ $message }}&lt;/p&gt;
                      @enderror
                      &lt;div class=&quot;flex mt-3&quot;&gt;
                          &lt;div @click=&quot;reply = false&quot;
                              class=&quot;bg-red-600 rounded-lg ml-3 text-white px-3 py-2 cursor-pointer&quot;&gt;
                              Cancel
                          &lt;/div&gt;
                          &lt;button wire:loading.remove wire:target=&quot;SubmitComment&quot; type=&quot;submit&quot;
                              class=&quot;inline-block bg-gray-800 hover:bg-gray-900  transition duration-300 px-3 py-2  text-white rounded-md&quot;&gt;
                              Send
                          &lt;/button&gt;
                          &lt;button wire:loading=&quot;&quot; wire:target=&quot;SubmitComment&quot; type=&quot;button&quot;
                              class=&quot;flex bg-gray-800 hover:bg-gray-900  transition duration-300 px-3 py-2  text-white rounded-md&quot;
                              disabled&gt;
                              &lt;span class=&quot;flex justify-center&quot;&gt;
                                  &lt;svg class=&quot;animate-spin w-6 h-6&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; fill=&quot;none&quot;
                                      viewBox=&quot;0 0 24 24&quot;&gt;
                                      &lt;circle class=&quot;opacity-25&quot; cx=&quot;12&quot; cy=&quot;12&quot; r=&quot;10&quot; stroke=&quot;currentColor&quot;
                                          stroke-width=&quot;4&quot;&gt;&lt;/circle&gt;
                                      &lt;path class=&quot;opacity-75&quot; fill=&quot;currentColor&quot;
                                          d=&quot;M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z&quot;&gt;
                                      &lt;/path&gt;
                                  &lt;/svg&gt;
                              &lt;/span&gt;
                          &lt;/button&gt;
                      &lt;/div&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
              &lt;div class=&quot;my-3&quot;&gt;
                  @include('partials.comment',['comments'=&gt;$comment-&gt;reply])
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  @endforeach
  
  </code></pre>