瀏覽代碼

Fix n+1 comment + like counts in status overlays

Daniel Supernault 7 年之前
父節點
當前提交
3d657e0c52
共有 2 個文件被更改,包括 8 次插入3 次删除
  1. 6 1
      app/Http/Controllers/ProfileController.php
  2. 2 2
      resources/views/profile/show.blade.php

+ 6 - 1
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'));
     }

+ 2 - 2
resources/views/profile/show.blade.php

@@ -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>