title: Trying to get property of non-object ErrorException Laravel
body: <p>I'm new to laravel and created a mini program using a bit of help of twitter bootstrap. </p>
  
  <p>I'm trying to access this route:</p>
  
  <pre><code>http://127.0.0.1:8000/posts/create
  </code></pre>
  
  <p>And I get these <strong>errors</strong>:</p>
  
  <blockquote>
    <p>2/2 ErrorException in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4:
    Trying to get property of non-object (View: C:\xampp\htdocs\laravel\alquirozlaravel\resources\views\show-solo.blade.php)</p>
  </blockquote>
  
  <pre><code>in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
  at CompilerEngine-&gt;handleViewException(object(ErrorException), 1) in PhpEngine.php line 44
  at PhpEngine-&gt;evaluatePath('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', array('__env' =&gt; object(Factory), 'app' =&gt; object(Application), 'errors' =&gt; object(ViewErrorBag), 'post' =&gt; null)) in CompilerEngine.php line 59
  at CompilerEngine-&gt;get('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\resources\\views/show-solo.blade.php', array('__env' =&gt; object(Factory), 'app' =&gt; object(Application), 'errors' =&gt; object(ViewErrorBag), 'post' =&gt; null)) in View.php line 137
  at View-&gt;getContents() in View.php line 120
  at View-&gt;renderContents() in View.php line 85
  at View-&gt;render() in Response.php line 38
  at Response-&gt;setContent(object(View)) in Response.php line 225
  at Response-&gt;__construct(object(View)) in Router.php line 602
  at Router-&gt;prepareResponse(object(Request), object(View)) in Router.php line 559
  at Router-&gt;Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
  at SubstituteBindings-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
  at VerifyCsrfToken-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
  at ShareErrorsFromSession-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
  at StartSession-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
  at AddQueuedCookiesToResponse-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
  at EncryptCookies-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
  at Pipeline-&gt;then(object(Closure)) in Router.php line 561
  at Router-&gt;runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
  at Router-&gt;dispatchToRoute(object(Request)) in Router.php line 498
  at Router-&gt;dispatch(object(Request)) in Kernel.php line 174
  at Kernel-&gt;Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
  at TransformsRequest-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
  at TransformsRequest-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
  at ValidatePostSize-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
  at CheckForMaintenanceMode-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
  at Pipeline-&gt;then(object(Closure)) in Kernel.php line 149
  at Kernel-&gt;sendRequestThroughRouter(object(Request)) in Kernel.php line 116
  at Kernel-&gt;handle(object(Request)) in index.php line 53
  at require_once('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\public\\index.php') in server.php line 21
  </code></pre>
  
  <blockquote>
    <p>1/2 ErrorException in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4:
    Trying to get property of non-object</p>
  </blockquote>
  
  <pre><code>in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
  at HandleExceptions-&gt;handleError(8, 'Trying to get property of non-object', 'C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views\\73427cb411f683691ba00d0846f7eda3c61bff74.php', 4, array('__path' =&gt; 'C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', '__data' =&gt; array('__env' =&gt; object(Factory), 'app' =&gt; object(Application), 'errors' =&gt; object(ViewErrorBag), 'post' =&gt; null), 'obLevel' =&gt; 1, '__env' =&gt; object(Factory), 'app' =&gt; object(Application), 'errors' =&gt; object(ViewErrorBag), 'post' =&gt; null)) in 73427cb411f683691ba00d0846f7eda3c61bff74.php line 4
  at include('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views\\73427cb411f683691ba00d0846f7eda3c61bff74.php') in PhpEngine.php line 42
  at PhpEngine-&gt;evaluatePath('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\storage\\framework\\views/73427cb411f683691ba00d0846f7eda3c61bff74.php', array('__env' =&gt; object(Factory), 'app' =&gt; object(Application), 'errors' =&gt; object(ViewErrorBag), 'post' =&gt; null)) in CompilerEngine.php line 59
  at CompilerEngine-&gt;get('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\resources\\views/show-solo.blade.php', array('__env' =&gt; object(Factory), 'app' =&gt; object(Application), 'errors' =&gt; object(ViewErrorBag), 'post' =&gt; null)) in View.php line 137
  at View-&gt;getContents() in View.php line 120
  at View-&gt;renderContents() in View.php line 85
  at View-&gt;render() in Response.php line 38
  at Response-&gt;setContent(object(View)) in Response.php line 225
  at Response-&gt;__construct(object(View)) in Router.php line 602
  at Router-&gt;prepareResponse(object(Request), object(View)) in Router.php line 559
  at Router-&gt;Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
  at SubstituteBindings-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
  at VerifyCsrfToken-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
  at ShareErrorsFromSession-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
  at StartSession-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
  at AddQueuedCookiesToResponse-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
  at EncryptCookies-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
  at Pipeline-&gt;then(object(Closure)) in Router.php line 561
  at Router-&gt;runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
  at Router-&gt;dispatchToRoute(object(Request)) in Router.php line 498
  at Router-&gt;dispatch(object(Request)) in Kernel.php line 174
  at Kernel-&gt;Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
  at TransformsRequest-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
  at TransformsRequest-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
  at ValidatePostSize-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
  at CheckForMaintenanceMode-&gt;handle(object(Request), object(Closure)) in Pipeline.php line 148
  at Pipeline-&gt;Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
  at Pipeline-&gt;Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
  at Pipeline-&gt;then(object(Closure)) in Kernel.php line 149
  at Kernel-&gt;sendRequestThroughRouter(object(Request)) in Kernel.php line 116
  at Kernel-&gt;handle(object(Request)) in index.php line 53
  at require_once('C:\\xampp\\htdocs\\laravel\\alquirozlaravel\\public\\index.php') in server.php line 21
  </code></pre>
  
  <p>Here are my <strong>routes</strong> at the moment:</p>
  
  <pre><code>Route::get('/posts', 'PostController@index');
  Route::get('/posts/{id}', 'PostController@showById');
  Route::get('/posts/create', 'PostController@showForm');
  Route::post('/posts', 'PostController@store');
  
  Route::post('/posts/{post}/comments', 'CommentController@store');
  </code></pre>
  
  <p>My <strong>Controllers</strong>:
  (PostController)</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Post;
  
  class PostController extends Controller
  {
      public function index() {
          $posts = Post::latest()-&gt;get();
  
          return view('index', compact('posts'));
      }
  
      public function showById($id)
      {
          $post = Post::find($id);
  
          return view('show-solo', compact('post'));
      }
  
      public function showForm()
      {
          return view('new-post');
      }
  
      public function store(Request $request) {
          $this-&gt;validate(request(), [
              'title' =&gt; 'required|min:2',
              'body' =&gt; 'required|min:10'
          ]);
  
          Post::create([
              'title' =&gt; $request-&gt;title,
              'body' =&gt; $request-&gt;body
          ]);
  
          return redirect('/posts');
      }
  }
  </code></pre>
  
  <p>(CommentController)</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Post;
  use App\Comment;
  
  class CommentController extends Controller
  {
      // Convention to POST ADD
      public function store(Post $post)
      {
          Comment::create([
              'body' =&gt; request('body'),
              'post_id' =&gt; $post-&gt;id
          ]);
  
          // return back() works too
          return back();
      }
  }
  </code></pre>
  
  <p>The <strong>Blades</strong> in question i think:
  (show-solo.blade.php)</p>
  
  <pre><code>@extends('master')
  
  @section('content')
      &lt;div class="col-sm-8 blog-main"&gt;
          &lt;div class="blog-post"&gt;
              &lt;h2&gt;Post number: {{ $post-&gt;id }}&lt;/h2&gt;
  
              &lt;h2 class="blog-post-title"&gt;
                  &lt;a class="title-link" href="/posts/{{ $post-&gt;id }}"&gt;{{ $post-&gt;title }}&lt;/a&gt;
              &lt;/h2&gt;
  
              &lt;!-- {{ $post-&gt;created_at-&gt;toFormattedDateString() }} --&gt;
              &lt;p class="blog-post-meta"&gt;{{ $post-&gt;created_at-&gt;diffForHumans() }} by &lt;a href="#"&gt;Mark&lt;/a&gt;&lt;/p&gt;
              {{ $post-&gt;body }}
  
              &lt;hr /&gt;
  
              @include('partials.error')
  
              @include('partials.post-comment')
          &lt;/div&gt;
      &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>(new-post.blade.php)</p>
  
  <pre><code>@extends('master')
  
  @section('content')
      &lt;div class="col-sm-8 blog-main"&gt;
          &lt;h1&gt;Publish a Post&lt;hr /&gt;&lt;/h1&gt;
  
          &lt;form method="POST" action="/posts"&gt;
              {{ csrf_field() }}
  
              &lt;div class="form-group"&gt;
                  &lt;label for="title"&gt;Title:&lt;/label&gt;
                  &lt;input type="text" class="form-control" id="title" name="title"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="body"&gt;Body&lt;/label&gt;
                  &lt;textarea name="body" id="body" class="form-control" cols="30" rows="10"&gt;&lt;/textarea&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;button type="submit" class="btn btn-primary"&gt;Publish&lt;/button&gt;
              &lt;/div&gt;
  
              @include('partials.error')
          &lt;/form&gt;
      &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>(master.blade.php)</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
      &lt;meta name="description" content=""&gt;
      &lt;meta name="author" content=""&gt;
  
      &lt;title&gt;Blog Template for Bootstrap&lt;/title&gt;
  
      &lt;!-- Bootstrap core CSS --&gt;
      &lt;link rel="stylesheet" href="{{ URL::asset('css/bootstrap.min.css') }}"&gt;
  
      &lt;!-- Custom styles for this template --&gt;
      &lt;link rel="stylesheet" href="{{ URL::asset('css/blog.css') }}"&gt;
      &lt;link rel="stylesheet" href="{{ URL::asset('css/style.css') }}"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;      
      @include('partials.nav')
  
      &lt;div class="container"&gt;
  
          &lt;div class="row"&gt;
              @yield('content')
  
              @include('partials.sidebar')
  
          &lt;/div&gt;&lt;!-- /.row --&gt;
  
      &lt;/div&gt;&lt;!-- /.container --&gt;
  
      @include('partials.footer')
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here are my <strong>models</strong> if it helps:
  (Post.php)</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  class Post extends Model
  {
      public function comments()
      {
          return $this-&gt;hasMany(Comment::class);
      }
  }
  </code></pre>
  
  <p>(Comment.php)</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  class Comment extends Model
  {
      public function post()
      {
          return $this-&gt;belongsTo(Post::class);
      }
  }
  </code></pre>
  
  <p>Before the posts/create route works fine and now it won't. When I try to remove the route Route::get('/posts/{id}', 'PostController@showById'); the post/create route works properly again. What Am I missing? Any help would be greatly appreciated, Thanks.</p>