Fix n+1 comment + like counts in status overlays

This commit is contained in:
Daniel Supernault 2018-06-02 20:26:49 -06:00
parent 3fcb53c7a6
commit 3d657e0c52
2 changed files with 8 additions and 3 deletions

View file

@ -32,7 +32,12 @@ class ProfileController extends Controller
// TODO: refactor this mess
$owner = Auth::check() && Auth::id() === $user->user_id;
$following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false;
$timeline = $user->statuses()->whereHas('media')->whereNull('in_reply_to_id')->orderBy('id','desc')->paginate(21);
$timeline = $user->statuses()
->whereHas('media')
->whereNull('in_reply_to_id')
->orderBy('id','desc')
->withCount(['comments', 'likes'])
->simplePaginate(21);
return view('profile.show', compact('user', 'owner', 'following', 'timeline'));
}

View file

@ -106,10 +106,10 @@
<div class="info-overlay-text">
<h5 class="text-white m-auto font-weight-bold">
<span class="pr-4">
<span class="icon-heart pr-1"></span> {{$status->likes()->count()}}
<span class="icon-heart pr-1"></span> {{$status->likes_count}}
</span>
<span>
<span class="icon-speech pr-1"></span> {{$status->comments()->count()}}
<span class="icon-speech pr-1"></span> {{$status->comments_count}}
</span>
</h5>
</div>