Skip to content

Commit 2d2df61

Browse files
authored
Fix n plus query issue with likes (#1345)
1 parent fe814a8 commit 2d2df61

File tree

4 files changed

+162
-6
lines changed

4 files changed

+162
-6
lines changed

app/Livewire/Likes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function mount($subject): void
3333

3434
public function getLikers(): array
3535
{
36-
$likers = $this->subject->likersRelation()->limit($this->limit + 1)->pluck('username')->toArray();
36+
$likers = $this->subject->likes()->slice($this->limit + 1)->pluck('username')->toArray();
3737

3838
if (auth()->check() && in_array($authUsername = auth()->user()->username, $likers)) {
3939
$likers = array_diff($likers, [$authUsername]);

app/Models/Thread.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ public static function feedByTagQuery(Tag $tag): Builder
294294
*/
295295
public static function feedQuery(): Builder
296296
{
297-
return self::withOnly([
298-
'tagsRelation',
299-
'authorRelation',
300-
])
297+
return self::query()
301298
->withCount(['repliesRelation as reply_count', 'likesRelation as like_count'])
302299
->latest('last_activity_at');
303300
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"symfony/mailgun-mailer": "^7.0"
4141
},
4242
"require-dev": {
43+
"barryvdh/laravel-debugbar": "^3.16",
4344
"fakerphp/faker": "^1.23",
4445
"mockery/mockery": "^1.6",
4546
"nunomaduro/collision": "^8.0",

composer.lock

Lines changed: 159 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)