title: Undefined variable: posts (View: C:\xampp\htdocs\practiseapp\resources\views\dashboard.blade.php)
body: <p>I am new to laravel in here i am just creating a simple app in which user can login and register and after that write post in dashboard after logging in, I am trying to display user posts data in dashboard which is saved in database, But i am getting this error:</p>
  
  <p>Undefined variable: posts (View: C:\xampp\htdocs\practiseapp\resources\views\dashboard.blade.php)</p>
  
  <p>i think i have defined the posts,I can't figure out what's wrong,Any help would be appreciated Thanks....</p>
  
  <p>My dashboard:</p>
  
  <pre><code>@extends('layout.master')
  
  @section('content')
  &lt;div class="col-sm-6 col-sm-offset-3"&gt;
      &lt;header&gt;&lt;h3&gt;What do you wanna say&lt;/h3&gt;&lt;/header&gt;
      &lt;form method="post" action="{{route('post.create')}}"&gt;
          {{csrf_field()}}
          &lt;div class="panel-body"&gt;
          &lt;div class="form-group"&gt;
              &lt;textarea class="form-control" name="body" id="body" rows="5" placeholder="Enter your post"&gt;
  
              &lt;/textarea&gt;
          &lt;/div&gt;
          &lt;button type="submit" class="btn btn-primary"&gt;Create post&lt;/button&gt;
  
      &lt;/form&gt;
      &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;section class="row-posts"&gt;
      &lt;div class="col-md-6 col-sm-offset-3"&gt;
          &lt;header&gt;&lt;h3&gt;Other people posts&lt;/h3&gt;&lt;/header&gt;
  
          @foreach($posts as $post)
          &lt;article class="post"&gt;
              &lt;p&gt;{{ $post-&gt;body }}&lt;/p&gt;
              &lt;div class="info"&gt;
                  posted by {{ $post-&gt;user-&gt;name}} on {{$post-&gt;created_at}}
              &lt;/div&gt;
              &lt;div class="interaction"&gt;
                  &lt;a href="#"&gt;Like&lt;/a&gt;|
                  &lt;a href="#"&gt;Disike&lt;/a&gt;|
                  &lt;a href="#"&gt;Edit&lt;/a&gt;|
                  &lt;a href="#"&gt;Delete&lt;/a&gt;  
              &lt;/div&gt;
          &lt;/article&gt;
          @endforeach
  
  
      &lt;/div&gt;
  
  &lt;/section&gt;
  @endsection
  </code></pre>
  
  <p>PostController.php</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Http\Requests;
  use App\Post;
  use App\UserTypes;
  
  use Auth;
  use Hashids;
  use Redirect;
  use Illuminate\Http\Request;
  use Hash;
  
  class PostController extends Controller
  {
  
      public function show()
      {
          //Fetching all the posts from the database
          $posts = Post::all();
          return view('dashboard',['posts'=&gt; $posts]);
      }
  
      public function store(Request $request)
      {
          $this-&gt;validate($request,[
              'body' =&gt; 'required'
  
          ]);
  
          $post = new Post;
          $post-&gt;body = $request-&gt;body;
          $request-&gt;user()-&gt;posts()-&gt;save($post);
  
          return redirect()-&gt;route('dashboard');      
      }
  }
  </code></pre>