diff --git a/app/Http/Controllers/Forum/TagsController.php b/app/Http/Controllers/Forum/TagsController.php index 727688b05..a36af5906 100644 --- a/app/Http/Controllers/Forum/TagsController.php +++ b/app/Http/Controllers/Forum/TagsController.php @@ -7,6 +7,7 @@ use App\Models\Tag; use App\Models\Thread; use App\Models\User; +use Illuminate\Support\Facades\Cache; use Illuminate\View\View; class TagsController extends Controller @@ -35,8 +36,12 @@ public function show(Tag $tag): View } $tags = Tag::orderBy('name')->get(); - $topMembers = User::mostSolutionsInLastDays(365)->take(5)->get(); - $moderators = User::moderators()->get(); + $topMembers = Cache::remember('topMembers', now()->addHour(), function () { + return User::mostSolutionsInLastDays(365)->take(5)->get(); + }); + $moderators = Cache::remember('moderators', now()->addDay(), function () { + return User::moderators()->get(); + }); $canonical = canonical('forum.tag', [$tag->name, 'filter' => $filter]); return view('forum.overview', compact('threads', 'filter', 'tags', 'topMembers', 'moderators', 'canonical') + ['activeTag' => $tag]); diff --git a/app/Http/Controllers/Forum/ThreadsController.php b/app/Http/Controllers/Forum/ThreadsController.php index ad24a4c95..2ef0628bc 100644 --- a/app/Http/Controllers/Forum/ThreadsController.php +++ b/app/Http/Controllers/Forum/ThreadsController.php @@ -61,10 +61,10 @@ public function overview() } $tags = Tag::orderBy('name')->get(); - $topMembers = Cache::remember('topMembers', now()->addMinutes(30), function () { + $topMembers = Cache::remember('topMembers', now()->addHour(), function () { return User::mostSolutionsInLastDays(365)->take(5)->get(); }); - $moderators = Cache::remember('moderators', now()->addMinutes(30), function () { + $moderators = Cache::remember('moderators', now()->addDay(), function () { return User::moderators()->get(); }); $canonical = canonical('forum', ['filter' => $filter]); @@ -74,7 +74,7 @@ public function overview() public function show(Thread $thread) { - $moderators = Cache::remember('moderators', now()->addMinutes(30), function () { + $moderators = Cache::remember('moderators', now()->addDay(), function () { return User::moderators()->get(); }); diff --git a/app/Models/Thread.php b/app/Models/Thread.php index 14feb0c90..c06c84956 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -295,7 +295,7 @@ public static function feedByTagQuery(Tag $tag): Builder public static function feedQuery(): Builder { return self::query() - ->withCount(['repliesRelation as reply_count', 'likesRelation as like_count']) + ->select('threads.*') ->latest('last_activity_at'); } diff --git a/app/Models/User.php b/app/Models/User.php index ca946155e..c7a4dae8b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Query\JoinClause; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Auth; @@ -340,19 +341,23 @@ public function countSolutions(): int public function scopeMostSolutions(Builder $query, ?int $inLastDays = null) { - return $query->withCount(['replyAble as solutions_count' => function ($query) use ($inLastDays) { - $query->where('replyable_type', 'threads') - ->join('threads', function ($join) { - $join->on('threads.solution_reply_id', '=', 'replies.id') - ->on('threads.author_id', '!=', 'replies.author_id'); - }); - - if ($inLastDays) { - $query->where('replies.created_at', '>', now()->subDays($inLastDays)); - } - - return $query; - }]) + return $query + ->selectRaw('users.*, COUNT(DISTINCT replies.id) AS solutions_count') + ->join('replies', 'replies.author_id', '=', 'users.id') + ->join('threads', function (JoinClause $join) { + $join->on('threads.solution_reply_id', '=', 'replies.id') + ->on('threads.author_id', '!=', 'replies.author_id'); + }) + ->where(function ($query) use ($inLastDays) { + $query->where('replyable_type', 'threads'); + + if ($inLastDays) { + $query->where('replies.created_at', '>', now()->subDays($inLastDays)); + } + + return $query; + }) + ->groupBy('users.id') ->having('solutions_count', '>', 0) ->orderBy('solutions_count', 'desc'); }