title: Laravel 5: Fetch comments
body: <p>I am building a blog commenting system.</p>
  
  <p>I want to show user's comments in show.blade.php.</p>
  
  <p>In my ResultsController I executed </p>
  
  <blockquote>
    <p>dd($post->comments); </p>
  </blockquote>
  
  <p>But I cannot retrieve any comments from my database.</p>
  
  <p>I seeded four comments to a post. 
  And user can reply to each one comment.</p>
  
  <p>migration table</p>
  
  <pre><code>Schema::create('comments', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;integer('user_id');
          $table-&gt;integer('post_id');
          $table-&gt;text('body');
          $table-&gt;integer('comment_id')-&gt;nullable();
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p>Comment.php</p>
  
  <pre><code>public function posts()
  {
      return $this-&gt;belongsTo(Post::class);
  }
  
  public function user()
  {
      return $this-&gt;belongsTo(User::class);
  }
  
  public function replies()
  {
      return $this-&gt;hasMany(Comment::class, 'comment_id')-&gt;whereNotNull('comment_id');
  }
  </code></pre>
  
  <p>Post.php</p>
  
  <pre><code> public function comments()
  {
      return $this-&gt;hasMany(Comment::class)-&gt;whereNull('comment_id');
  }
  </code></pre>
  
  <p>User.php</p>
  
  <pre><code>public function comments()
  {
      return $this-&gt;hasMany(Comment::class);
  }
  </code></pre>
  
  <p>Mysql
  <a href="https://i.stack.imgur.com/ayCJC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ayCJC.png" alt="I seeded 4 comments to a post(id=52)"></a></p>
  
  <p>ResultsController.php</p>
  
  <pre><code> public function show($id,Post $post)
  {
      $particular_post= Post::find($id);
      $recommended_posts = Post::latest()
                              -&gt;whereDate('date','&gt;',date('Y-m-d'))
                              -&gt;where('category_id','=',$particular_post-&gt;category_id)
                              -&gt;where('id','!=',$particular_post-&gt;id)
                              -&gt;limit(7)
                              -&gt;get();
  
      $posts['particular_post'] = $particular_post;
      $posts['recommended_posts'] = $recommended_posts;
  
      dd($post-&gt;comments);
  
      return view('posts.show',compact('posts'));
  }
  </code></pre>
  
  <p><a href="http://127.0.0.1:8000/results/52" rel="nofollow noreferrer">http://127.0.0.1:8000/results/52</a> 
  This is the URL which supposed to fetch comments.</p>