【问题标题】:Laravel 5.5 Trying to get property of non object - after make:auth commandLaravel 5.5 试图获取非对象的属性 - 在 make:auth 命令之后
【发布时间】:2017-10-28 14:06:33
【问题描述】:

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

完全错误:试图获取非对象的属性(查看:/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 刀片文件(简单引导示例)

<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>

我哪里错了?

【问题讨论】:

  • 你需要向我们展示/home/vagrant/www/blog/resources/views/front/pages/single.blade.php的内容,我不知道,错误你很清楚,如果你不知道在执行期间搜索-&gt; some object is null。
  • @Kyslik ,编辑并添加了 single.blade.php
  • 请在您的BlogController 中向我们展示getSingle 方法。您也可以尝试死转储 (dd) 您的 $post 变量以查看其中的内容。
  • @mrHooty 也添加了 getSingle。我是初学者,我不知道我该怎么做那件死事..
  • Laravel 提供了一个dd() 方法,它转储给定的值并让您的应用程序在到达该点时终止。使用它会在您的浏览器中为您提供一个不错的对象浏览器,您可以使用它来检查您的对象。您可以使用它来检查您的 $post 变量。您能否也添加您的front.master 模板?

标签: php laravel laravel-5.5


【解决方案1】:
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>

【讨论】:

    【解决方案2】:

    尝试在您的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
    

    【讨论】:

    • 如果这不起作用,请将您的index() 方法的代码包含在您的 HomeController 中。
    猜你喜欢
    • 2018-06-06
    • 1970-01-01
    • 2018-07-03
    • 2018-03-03
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多