From 478521bb38bfd7976150d37e4ff1536e6a66e9a4 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Wed, 3 Sep 2025 21:17:18 +0200 Subject: [PATCH] Fix n plus query issue with likes --- app/Livewire/Likes.php | 2 +- app/Models/Thread.php | 5 +- composer.json | 1 + composer.lock | 160 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 162 insertions(+), 6 deletions(-) diff --git a/app/Livewire/Likes.php b/app/Livewire/Likes.php index 1aca23a4e..fbc583b3e 100644 --- a/app/Livewire/Likes.php +++ b/app/Livewire/Likes.php @@ -33,7 +33,7 @@ public function mount($subject): void public function getLikers(): array { - $likers = $this->subject->likersRelation()->limit($this->limit + 1)->pluck('username')->toArray(); + $likers = $this->subject->likes()->slice($this->limit + 1)->pluck('username')->toArray(); if (auth()->check() && in_array($authUsername = auth()->user()->username, $likers)) { $likers = array_diff($likers, [$authUsername]); diff --git a/app/Models/Thread.php b/app/Models/Thread.php index 1d19a39bb..14feb0c90 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -294,10 +294,7 @@ public static function feedByTagQuery(Tag $tag): Builder */ public static function feedQuery(): Builder { - return self::withOnly([ - 'tagsRelation', - 'authorRelation', - ]) + return self::query() ->withCount(['repliesRelation as reply_count', 'likesRelation as like_count']) ->latest('last_activity_at'); } diff --git a/composer.json b/composer.json index f67965d96..1bb74509c 100644 --- a/composer.json +++ b/composer.json @@ -40,6 +40,7 @@ "symfony/mailgun-mailer": "^7.0" }, "require-dev": { + "barryvdh/laravel-debugbar": "^3.16", "fakerphp/faker": "^1.23", "mockery/mockery": "^1.6", "nunomaduro/collision": "^8.0", diff --git a/composer.lock b/composer.lock index db9a83443..beee99901 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fd53a824d0eb289a65ca8e5ad86e829b", + "content-hash": "f03a71a515e66b00b806bb7df6481872", "packages": [ { "name": "abraham/twitteroauth", @@ -10430,6 +10430,91 @@ } ], "packages-dev": [ + { + "name": "barryvdh/laravel-debugbar", + "version": "v3.16.0", + "source": { + "type": "git", + "url": "https://github.com/barryvdh/laravel-debugbar.git", + "reference": "f265cf5e38577d42311f1a90d619bcd3740bea23" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barryvdh/laravel-debugbar/zipball/f265cf5e38577d42311f1a90d619bcd3740bea23", + "reference": "f265cf5e38577d42311f1a90d619bcd3740bea23", + "shasum": "" + }, + "require": { + "illuminate/routing": "^9|^10|^11|^12", + "illuminate/session": "^9|^10|^11|^12", + "illuminate/support": "^9|^10|^11|^12", + "php": "^8.1", + "php-debugbar/php-debugbar": "~2.2.0", + "symfony/finder": "^6|^7" + }, + "require-dev": { + "mockery/mockery": "^1.3.3", + "orchestra/testbench-dusk": "^7|^8|^9|^10", + "phpunit/phpunit": "^9.5.10|^10|^11", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "extra": { + "laravel": { + "aliases": { + "Debugbar": "Barryvdh\\Debugbar\\Facades\\Debugbar" + }, + "providers": [ + "Barryvdh\\Debugbar\\ServiceProvider" + ] + }, + "branch-alias": { + "dev-master": "3.16-dev" + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Barryvdh\\Debugbar\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "PHP Debugbar integration for Laravel", + "keywords": [ + "debug", + "debugbar", + "dev", + "laravel", + "profiler", + "webprofiler" + ], + "support": { + "issues": "https://github.com/barryvdh/laravel-debugbar/issues", + "source": "https://github.com/barryvdh/laravel-debugbar/tree/v3.16.0" + }, + "funding": [ + { + "url": "https://fruitcake.nl", + "type": "custom" + }, + { + "url": "https://github.com/barryvdh", + "type": "github" + } + ], + "time": "2025-07-14T11:56:43+00:00" + }, { "name": "brianium/paratest", "version": "v7.8.3", @@ -11575,6 +11660,79 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "php-debugbar/php-debugbar", + "version": "v2.2.4", + "source": { + "type": "git", + "url": "https://github.com/php-debugbar/php-debugbar.git", + "reference": "3146d04671f51f69ffec2a4207ac3bdcf13a9f35" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-debugbar/php-debugbar/zipball/3146d04671f51f69ffec2a4207ac3bdcf13a9f35", + "reference": "3146d04671f51f69ffec2a4207ac3bdcf13a9f35", + "shasum": "" + }, + "require": { + "php": "^8", + "psr/log": "^1|^2|^3", + "symfony/var-dumper": "^4|^5|^6|^7" + }, + "replace": { + "maximebf/debugbar": "self.version" + }, + "require-dev": { + "dbrekelmans/bdi": "^1", + "phpunit/phpunit": "^8|^9", + "symfony/panther": "^1|^2.1", + "twig/twig": "^1.38|^2.7|^3.0" + }, + "suggest": { + "kriswallsmith/assetic": "The best way to manage assets", + "monolog/monolog": "Log using Monolog", + "predis/predis": "Redis storage" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.1-dev" + } + }, + "autoload": { + "psr-4": { + "DebugBar\\": "src/DebugBar/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Maxime Bouroumeau-Fuseau", + "email": "maxime.bouroumeau@gmail.com", + "homepage": "http://maximebf.com" + }, + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "Debug bar in the browser for php application", + "homepage": "https://github.com/php-debugbar/php-debugbar", + "keywords": [ + "debug", + "debug bar", + "debugbar", + "dev" + ], + "support": { + "issues": "https://github.com/php-debugbar/php-debugbar/issues", + "source": "https://github.com/php-debugbar/php-debugbar/tree/v2.2.4" + }, + "time": "2025-07-22T14:01:30+00:00" + }, { "name": "phpdocumentor/reflection-common", "version": "2.2.0",