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
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. 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($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