Fix n+1 query in status view

This commit is contained in:
Daniel Supernault 2018-06-03 01:15:38 -06:00
parent 7f7e8526f3
commit 6d5c8fb90a
2 changed files with 4 additions and 2 deletions

View File

@ -13,7 +13,9 @@ class StatusController extends Controller
public function show(Request $request, $username, int $id) public function show(Request $request, $username, int $id)
{ {
$user = Profile::whereUsername($username)->firstOrFail(); $user = Profile::whereUsername($username)->firstOrFail();
$status = Status::whereProfileId($user->id)->findOrFail($id); $status = Status::whereProfileId($user->id)
->withCount('likes')
->findOrFail($id);
if(!$status->media_path && $status->in_reply_to_id) { if(!$status->media_path && $status->in_reply_to_id) {
return view('status.reply', compact('user', 'status')); return view('status.reply', compact('user', 'status'));
} }

View File

@ -65,7 +65,7 @@
</span> </span>
</div> </div>
<div class="likes font-weight-bold mb-0"> <div class="likes font-weight-bold mb-0">
<span class="like-count">{{$status->likes()->count()}}</span> likes <span class="like-count">{{$status->likes_count}}</span> likes
</div> </div>
</div> </div>
<div class="card-footer"> <div class="card-footer">