title: laravel 5.6 fetching data from database to view
body: <p>I'm facing an issue on understanding the content of the index function in the controller </p>
  
  <p>I'm trying to get all the posts from my posts table into a view</p>
  
  <p>my index function </p>
  
  <pre><code>public function index()
  {
      $posts=\App\post::all();
      return view('MainViews.welcome',compact('posts'));
  }
  </code></pre>
  
  <p>my view </p>
  
  <pre><code>   &lt;div class="row"&gt;
      @foreach($posts as $post)
        &lt;div class='col-md-8'&gt;
          &lt;div class="post"&gt;&lt;/div&gt;
               h3&gt;{{$post-&gt;subject}}&lt;/h3&gt;
            &lt;a href="#" class="btn btn-primary"&gt;read it&lt;/a&gt;
            &lt;p&gt;
            &lt;/p&gt;
          &lt;/div&gt;
        @endforeach
       &lt;/div&gt;
  </code></pre>
  
  <p>I'm getting Undefined variable error and highlighting @foreach</p>
  
  <p>below is error highlighted </p>
  
  <pre><code>          &lt;div class="row"&gt;
              18.    &lt;?php $__currentLoopData = $posts; $__env-&gt;addLoop ($__currentLoopData); foreach($__currentLoopData as $posts):  $__env-&gt;incrementLoopIndices(); $loop = $__env-&gt;getLastLoop(); ?&gt;
             19.          &lt;div class='col-md-8'&gt;
              20.            &lt;div class="post"&gt;&lt;/div&gt;
              21.              &lt;h3&gt;&lt;?php echo e($posts-&gt;subject); ?&gt;&lt;/h3&gt;
              22.              &lt;a href="#" class="btn btn-primary"&gt;read it&lt;/a&gt;
               23.              &lt;p&gt;
               24.              &lt;/p&gt;
                25.            &lt;/div&gt;
                 26.  &lt;?php endforeach; $__env-&gt;popLoop(); $loop = $__env-&gt;getLastLoop(); ?&gt;
                27. 
                 28.          &lt;/div&gt;
                  29.&lt;?php $__env-&gt;stopSection(); ?&gt;
  </code></pre>