【发布时间】: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">
<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">
<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">
<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' => null, 'best_answer' => null。尝试dd($d);和dd($best_answer); -
都返回 null
-
这就是你得到错误的原因!根据您的查询,该记录不存在!
-
我没有提到我在发新帖子时收到此错误,我认为 best_answer 和讨论默认为空
-
谢谢你,我已经通过添加这个 if 语句来修复它 if (!$d) { $this->store(); session()->flash('错误', '未找到讨论。');返回重定向()->返回(); }
标签: php laravel web-applications