|
12 | 12 | use Illuminate\Foundation\Auth\User as Authenticatable; |
13 | 13 | use Illuminate\Notifications\Notifiable; |
14 | 14 | use Illuminate\Support\Facades\Auth; |
| 15 | +use Illuminate\Support\Facades\Cache; |
15 | 16 | use Spatie\MediaLibrary\HasMedia; |
16 | 17 | use Spatie\MediaLibrary\InteractsWithMedia; |
17 | 18 | use Spatie\Permission\Traits\HasRoles; |
@@ -292,19 +293,34 @@ public function routeNotificationForSlack($notification): string |
292 | 293 | return env('SLACK_WEBHOOK_URL', ''); |
293 | 294 | } |
294 | 295 |
|
295 | | - public function countReplies(): int |
| 296 | + public function replies(): Collection |
296 | 297 | { |
297 | | - return $this->replyAble()->count(); |
| 298 | + return $this->replyAble; |
298 | 299 | } |
299 | 300 |
|
300 | | - public function replies(): Collection |
| 301 | + public function countReplies(): int |
301 | 302 | { |
302 | | - return $this->replyAble; |
| 303 | + return Cache::remember('replies_count', now()->addHours(2), fn () => $this->replyAble()->count()); |
303 | 304 | } |
304 | 305 |
|
305 | 306 | public function countSolutions(): int |
306 | 307 | { |
307 | | - return $this->replyAble()->isSolution()->count(); |
| 308 | + return Cache::remember('solutions_count', now()->addHours(2), fn () => $this->replyAble()->isSolution()->count()); |
| 309 | + } |
| 310 | + |
| 311 | + public function countArticles(): int |
| 312 | + { |
| 313 | + return Cache::remember('articles_count', now()->addHours(2), fn () => $this->articles()->approved()->count()); |
| 314 | + } |
| 315 | + |
| 316 | + public function countDiscussions(): int |
| 317 | + { |
| 318 | + return Cache::remember('discussions_count', now()->addHours(2), fn () => $this->discussions()->count()); |
| 319 | + } |
| 320 | + |
| 321 | + public function countThreads(): int |
| 322 | + { |
| 323 | + return Cache::remember('threads_count', now()->addHours(2), fn () => $this->threads()->count()); |
308 | 324 | } |
309 | 325 |
|
310 | 326 | public function scopeMostSolutions(Builder $query, int $inLastDays = null) |
@@ -345,6 +361,7 @@ public function scopeMostSubmissionsInLastDays(Builder $query, int $days) |
345 | 361 | public function scopeWithCounts(Builder $query) |
346 | 362 | { |
347 | 363 | return $query->withCount([ |
| 364 | + 'articles as articles_count', |
348 | 365 | 'threads as threads_count', |
349 | 366 | 'replyAble as replies_count', |
350 | 367 | 'replyAble as solutions_count' => function (Builder $query) { |
|
0 commit comments