From 3d657e0c5259c22da67c46f9e7a6e6eab427cd2e Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 2 Jun 2018 20:26:49 -0600 Subject: [PATCH] Fix n+1 comment + like counts in status overlays --- app/Http/Controllers/ProfileController.php | 7 ++++++- resources/views/profile/show.blade.php | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index a9d9846c..6777dbb4 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -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')); } diff --git a/resources/views/profile/show.blade.php b/resources/views/profile/show.blade.php index ca716640..c2206387 100644 --- a/resources/views/profile/show.blade.php +++ b/resources/views/profile/show.blade.php @@ -106,10 +106,10 @@
- {{$status->likes()->count()}} + {{$status->likes_count}} - {{$status->comments()->count()}} + {{$status->comments_count}}