Laravel 5.5 尝试获取非 object 的 属性 - 在 make:auth 命令之后

Laravel 5.5 Trying to get property of non object - after make:auth command

我使用了make:auth命令进行身份验证。但是当我打开“/home”url 时,它给了我那个错误。我不知道那是什么,我该如何解决。

Full Error: Trying to get property of non-object (View: /home/vagrant/www/blog/resources/views/front/pages/single.blade.php)

(我使用 single.blade 来显示我的帖子)

路线:

Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
// also here is my single route
Route::get('/{slug}', 'BlogController@getSingle')->name('post_slug');

Single.blade.php

@extends('front.master')
@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-9">

                <div class="blog-post">
                    <h2 class="blog-post-title">{{$post->title}}</h2>
                    <p class="blog-post-meta">{{$post->created_at}} </p>
                    <hr/>
                    <div class="blog-main">
                        {{$post->body}}
                    </div>
                </div>

            </div>

        </div>
@endsection

BlogController - getSingle

public function getSingle($slug){
        $post = Post::where('slug', $slug)->first();
        return view('front.pages.single')->with('post', $post);
    }

Front.master blade 文件(简单 bootstrap 示例)

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

    <title>Laravel 5 Blog Example</title>

    <!-- Bootstrap core CSS -->
    <link href="{{asset('/css/bootstrap.min.css')}}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="{{asset('/css/blog.css')}}" rel="stylesheet">
</head>
<body>

<div class="blog-masthead">
    <div class="container">
        <nav class="nav">
            <a class="{{Request::is('/') ? "nav-link active" : "nav-link"}}" href="{{route('home')}}">Anasayfa</a>
            <a class="{{Request::is('about') ? "nav-link active" : "nav-link"}}" href="{{route('about')}}">Aboust Us</a>
            <a class="{{Request::is('contact') ? "nav-link active" : "nav-link"}}" href="{{route('contact')}}">Contact Us</a>
            <a class="{{Request::is('home') ? "nav-link active" : "nav-link"}}" href="{{route('home')}}">Home</a>
            <a class="nav-link" href="{{url('admin/panel')}}">Admin</a>
        </nav>
    </div>
</div>
@yield('content')
<footer class="blog-footer">
    <p>
        Yeni Bir Blog Denemesi | Laravel 5.5 <br>
        <a href="#">Yukarı çık</a>
    </p>
</footer>


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="../../../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="../../../../assets/js/vendor/popper.min.js"></script>
<script src="../../../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../../../assets/js/ie10-viewport-bug-workaround.js"></script>

</html>

我哪里弄错了?

尝试在您的 Single.blade.php:

中使用它
@extends('front.master')
@section('content')
  @if(count($post) > 0)
    <div class="container">
        <div class="row">
            <div class="col-md-9">

                <div class="blog-post">
                    <h2 class="blog-post-title">{{$post->title}}</h2>
                    <p class="blog-post-meta">{{$post->created_at}} </p>
                    <hr/>
                    <div class="blog-main">
                        {{$post->body}}
                    </div>
                </div>

            </div>

        </div>
     </div>
   @else
     <h3>No post yet.</h3>
   @endif
@endsection
Route::get('/home', 'HomeController@index')->name('home');

在路由文件中使用下面的内容

Route::get('/', 'HomeController@index')->name('home');
<a class="{{Request::is('/') ? "nav-link active" : "nav-link"}}" href="{!! route('home') !!}">Anasayfa</a>