【问题标题】:laravel error: Trying to get property of non-object (\show.blade.php)laravel 错误:试图获取非对象的属性 (\show.blade.php)
【发布时间】:2018-08-03 03:30:17
【问题描述】:

我正在用 laravel 制作一个论坛应用程序,我遇到了这个错误:

试图获取非对象的属性(查看: C:\xampp\htdocs\fourm\resources\views\discussion\show.blade.php) 在 PhpEngine->evaluatePath('C:\xampp\htdocs\fourm\storage\framework\views/a2f2c494b8859e0552bfa22c40ceb4065b2efbe5.php', 数组('__env' => 对象(工厂),'app' => 对象(应用程序), 'channels' => object(Collection), 'errors' => object(ViewErrorBag), 'd' => null, 'best_answer' => null)) 在 CompilerEngine.php(第 59 行)

这是我的控制器代码:

public function show($slug)
{
     $discussion = Discussion::where('slug', $slug)->first();
     $best_answer = Reply::where('best_answer', 1)->first();
     return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
}

这是我的视图代码:

@extends('layouts.app')

@section('content')
    <div class="panel panel-default">
        <div class="panel-heading">
            <img src="{{ $d->user->avatar }}" alt="" width="40px" height="40px">&nbsp;&nbsp;&nbsp;
            <span>{{ $d->user->name }}, <b>( {{ $d->user->points }} )</b></span>
            @if($d->is_being_watch_by_user())
                <a href="{{ route('discussion.unwatch', ['id' => $d->id ]) }}" class="btn btn-default btn-xs pull-right">unwatch</a>
            @else
                <a href="{{ route('discussion.watch', ['id' => $d->id ]) }}" class="btn btn-default btn-xs pull-right">watch</a>
            @endif
        </div>

        <div class="panel-body">
            <h4 class="text-center">
                <b>{{ $d->title }}</b>
            </h4>
            <hr>
            <p class="text-center">
                {{ $d->content }}
            </p>

            <hr>

            @if($best_answer)
                <div class="text-center" style="padding: 40px;">
                    <h3 class="text-center">BEST ANSWER</h3>
                    <div class="panel panel-success">
                        <div class="panel-heading">
                            <img src="{{ $best_answer->user->avatar }}" alt="" width="40px" height="40px">&nbsp;&nbsp;&nbsp;
                            <span>{{ $best_answer->user->name }} <b>( {{ $best_answer->user->points }} )</b></span>
                        </div>

                        <div class="panel-body">
                            {{ $best_answer->content }}
                        </div>
                    </div>
                </div>
            @endif
        </div>
        <div class="panel-footer">
                    <span>
                        {{ $d->replies->count() }} Replies
                    </span>
            <a href="{{ route('channel', ['slug' => $d->channel->slug ]) }}" class="pull-right btn btn-default btn-xs">{{ $d->channel->title }}</a>
        </div>
    </div>

    @foreach($d->replies as $r)
        <div class="panel panel-default">
            <div class="panel-heading">
                <img src="{{ $r->user->avatar }}" alt="" width="40px" height="40px">&nbsp;&nbsp;&nbsp;
                <span>{{ $r->user->name }} <b>( {{ $r->user->points }} )</b></span>
                @if(!$best_answer)
                    @if(Auth::id() == $d->user->id)
                        <a href="{{ route('discussion.best.answer', ['id' => $r->id ]) }}" class="btn btn-xs btn-info pull-right">Mark as best answer</a>
                    @endif
                @endif
            </div>

            <div class="panel-body">
                <p class="text-center">
                    {{ $r->content }}
                </p>
            </div>
            <div class="panel-footer">
                @if($r->is_liked_by_user())
                    <a href="{{ route('reply.unlike', ['id' => $r->id ]) }}" class="btn btn-danger btn-xs">Unlike <span class="badge">{{ $r->likes->count() }}</span></a>
                @else
                    <a href="{{ route('reply.like', ['id' => $r->id ]) }}" class="btn btn-success btn-xs">Like <span class="badge">{{ $r->likes->count() }}</span></a>
                @endif
            </div>
        </div>
    @endforeach

    <div class="panel panel-default">
        <div class="panel-body">
            @if(Auth::check())
                <form action="{{ route('discussion.reply', ['id' => $d->id ]) }}" method="post">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label for="reply">Leave a reply...</label>
                        <textarea name="reply" id="reply" cols="30" rows="10" class="form-control"></textarea>
                    </div>

                    <div class="form-group">
                        <button class="btn pull-right">Leave a reply</button>
                    </div>
                </form>
            @else

                <div class="text-center">
                    <h2>Sign in to leave a reply</h2>
                </div>
            @endif
        </div>
    </div>
@endsection

【问题讨论】:

  • 在您的错误中,这会显示:'d' =&gt; null, 'best_answer' =&gt; null。尝试dd($d);dd($best_answer);
  • 都返回 null
  • 这就是你得到错误的原因!根据您的查询,该记录不存在!
  • 我没有提到我在发新帖子时收到此错误,我认为 best_answer 和讨论默认为空
  • 谢谢你,我已经通过添加这个 if 语句来修复它 if (!$d) { $this->store(); session()->flash('错误', '未找到讨论。');返回重定向()->返回(); }

标签: php laravel web-applications


【解决方案1】:

我通过在出现错误的页面上添加:&lt;?php error_reporting(0);?&gt; 解决了我的问题。 :D

【讨论】:

    【解决方案2】:

    确保您没有返回对象并将其作为数组访问。这可能是一个常见错误。

    【讨论】:

      【解决方案3】:

      您正在尝试使用 null 对象访问属性,这就是为什么您收到“尝试获取 null 对象的属性”之类的错误。

      根据我们在 cmets 中的讨论,您在两个查询中都获得了 null 值,这就是出错的原因!

      尝试添加if..else 条件如下:

      if (!$d) {   
           session()->flash('error', 'Discussion not found.'); 
           return redirect()->back(); 
      } else {
          return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
      }
      

      希望对您有所帮助!

      【讨论】:

        【解决方案4】:

        您收到此错误是因为两个查询都返回 null

        $discussion = Discussion::where('slug', $slug)->first();
        $best_answer = Reply::where('best_answer', 1)->first();
        

        因此,您需要使用 is_null()empty() 手动检查空结果,或者只是:

        if (!$discussion) {
            abort(404);
        }
        

        如果没有找到记录,或者使用findOrFail() 抛出异常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-13
          • 2018-06-02
          • 2017-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多