[英]Trying to get property author of non-object error, how to fix it?
Have tried to follow this tutorial Laravel show last reply left on post , was working but now returns on some threads with this error here已尝试按照本教程Laravel 显示最后的回复留在帖子上,正在工作,但现在在某些线程上返回此错误
Trying to get property 'author' of non-object (View: /var/www/html/web/resources/views/forums/board.blade.php)
This is my code used:这是我使用的代码:
<h1>{{$board->name}}</h1>
@auth
<a role="button" href="{{route('forums.thread.create', $board->id)}}" class="btn btn-primary">Create Thread</a>
@endauth
@foreach($board->threads->sortByDesc('pinned') as $thread)
<div class="thread-box {{$thread->pinned ? '' : 'bg-light'}} p-3 mt-3" style="background-color: #eee;">
<a href="{{route('forums.thread.show', $thread->id)}}" class="text-decoration-none">
<img src="{{$thread->author->avatar}}" alt="User Avatar" style="max-height: 40px;" class="rounded-circle">
<span>
{{$thread->name}}
</span>
</a>
@if(!$thread->pinned)
@else
<div class=" d-inline lock">
<i class="fas fa-thumbtack"></i>
</div>
@endif
@if(!$thread->locked)
@else
<div class=" d-inline lock">
<i class="fas fa-lock"></i>
</div>
@endif
<hr>
@if($thread->replies)<p>Last Update: <img src="{{$thread->replies->sortBydesc('id')->first()->author->avatar}}" alt="User Avatar" style="max-height: 40px;" class="rounded-circle"> <b>{{$thread->replies->sortBydesc('id')->first()->author->username}}</b></p>@else<p>No New Activity</p>@endif
</div>
@endforeach
</div>
@endsection
Was working before like I said, but does not seem to work anymore.以前像我说的那样工作,但似乎不再工作了。 Any ideas?
有任何想法吗?
$thread->replies->sortBydesc('id')->first()
is somehow not returning the latest reply you're looking for, even though you already check for $thread->replies
in the if statement. $thread->replies->sortBydesc('id')->first()
以某种方式没有返回您正在寻找的最新回复,即使您已经在 if 语句中检查了$thread->replies
。 I would debug why the latest reply for the thread can't be retrieved, or just add another condition in the if statement to check for that as well.我会调试为什么无法检索线程的最新回复,或者只是在 if 语句中添加另一个条件来检查它。
@if($thread->replies && $thread->replies->sortBydesc('id')->first())
<p>Last Update:
<img src="{{$thread->replies->sortBydesc('id')->first()->author->avatar}}"
alt="User Avatar" style="max-height: 40px;" class="rounded-circle">
<b>{{$thread->replies->sortBydesc('id')->first()->author->username}}</b>
</p>
@else
<p>No New Activity</p>
@endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.