title: How to link comments to each post by id with Laravel Blade
body: <p>I have created a Post and Comment application. I am trying to link the comments to the <code>id</code> of the post. I am trying to use PHP in a Blade template to do this. I get the error that the variable <code>post_id</code> does not exist. I want to use the <code>@if</code> and <code>@foreach</code> of Blade. The problem seems to be in the <code>@if</code> statement.</p>
  
  <p>Here is my HTML:</p>
  
  <pre><code>  &lt;div class="col-md-9"&gt;
        &lt;div class="row"&gt;
          @foreach($posts as $post)
            &lt;div class="col-md-12 post" id="post{{ $post-&gt;id }}"&gt;
                &lt;div class="row"&gt;
                    &lt;div class="col-md-12"&gt;
                        &lt;h4&gt;
                            {{ $post-&gt;title }}
                        &lt;/h4&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="row"&gt;
                    &lt;div class="col-md-12 post-header-line"&gt;
                        &lt;span class="glyphicon glyphicon-user"&gt;&lt;/span&gt; by &lt;a href="#"&gt;{{ $post-&gt;user-&gt;firstName }} {{ $post-&gt;user-&gt;lastName }}&lt;/a&gt; | &lt;span class="glyphicon glyphicon-calendar"&gt;
                        &lt;/span&gt; {{ $post-&gt;created_at }} | &lt;span class="glyphicon glyphicon-comment"&gt;&lt;/span&gt;&lt;a href="#"&gt;
                            3 Comments&lt;/a&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="row post-content"&gt;
                    &lt;div class="col-md-2"&gt;
                        &lt;a href="#"&gt;
                            &lt;img src="/images/random/postimg.jpg" alt="" class="img-responsive postImg"&gt;
                        &lt;/a&gt;
                    &lt;/div&gt;
                    &lt;div class="col-md-10"&gt;
                        &lt;p&gt;
                            {{ $post-&gt;post }}
                        &lt;/p&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="row add-comment"&gt;
                  &lt;div class="form-group"&gt;
                    &lt;input type="text" name="addComment" placeholder="Add your comment" class="form-control" v-model="comment"&gt;
                  &lt;/div&gt;
                  &lt;input id="addComment" type="submit" name="submitComment" value="Submit Comment" class="btn btn-default" v-on:click="submitComment" data-id="{{ $post-&gt;id }}"&gt;
  
                &lt;/div&gt;
  
                @if($post-&gt;comment-&gt;post_id == $post-&gt;id)
                  @foreach($post-&gt;comment as $comment)
  
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-12 mb-r"&gt;
                          &lt;div class="card example-1 scrollbar-ripe-malinka"&gt;
                              &lt;div class="card-body"&gt;
                                  &lt;h4 id="section1"&gt;&lt;strong&gt;By: {{ $comment-&gt;user-&gt;firstName }} {{ $comment-&gt;user-&gt;lastName }}&lt;/strong&gt;&lt;/h4&gt;
                                  &lt;p&gt;{{ $comment-&gt;comment }}&lt;/p&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  @endforeach
                @endif
            &lt;/div&gt;
            @endforeach
        &lt;/div&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>Here is my PostController:</p>
  
  <pre><code>  public function index(){
        $posts = Post::with('comment', 'user')-&gt;orderBy('id', 'desc')-&gt;limit(20)-&gt;get();
        return view('post.posts')-&gt;with('posts', $posts);
      }
  </code></pre>