Trying to get 属性 author of non-object 错误,如何解决?
Trying to get property author of non-object error, how to fix it?
已尝试按照本教程进行操作 Laravel show last reply left on post,一直有效,但现在 returns 在某些线程上出现此错误
Trying to get property 'author' of non-object (View: /var/www/html/web/resources/views/forums/board.blade.php)
这是我使用的代码:
<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
像我说的那样之前可以工作,但似乎不再工作了。有什么想法吗?
$thread->replies->sortBydesc('id')->first()
以某种方式没有返回您正在寻找的最新回复,即使您已经在 if 语句中检查了 $thread->replies
。我会调试为什么无法检索线程的最新回复,或者只是在 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
已尝试按照本教程进行操作 Laravel show last reply left on post,一直有效,但现在 returns 在某些线程上出现此错误
Trying to get property 'author' of non-object (View: /var/www/html/web/resources/views/forums/board.blade.php)
这是我使用的代码:
<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
像我说的那样之前可以工作,但似乎不再工作了。有什么想法吗?
$thread->replies->sortBydesc('id')->first()
以某种方式没有返回您正在寻找的最新回复,即使您已经在 if 语句中检查了 $thread->replies
。我会调试为什么无法检索线程的最新回复,或者只是在 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