Skip to content

Commit 5982458

Browse files
committed
⚡ correction des queries et du design dans le thread
1 parent 6ba5c81 commit 5982458

File tree

6 files changed

+19
-17
lines changed

6 files changed

+19
-17
lines changed

app/Http/Controllers/User/DashboardController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace App\Http\Controllers\User;
44

55
use App\Http\Controllers\Controller;
6+
use App\Models\User;
67
use Illuminate\Support\Facades\Auth;
78

89
class DashboardController extends Controller
910
{
1011
public function dashboard()
1112
{
1213
return view('user.dashboard', [
13-
'user' => $user = Auth::user(),
14+
'user' => $user = User::scopes('withCounts')->find(Auth::id()),
1415
'articles' => $user->articles()
1516
->orderByDesc('submitted_at')
1617
->orderByDesc('created_at')
@@ -21,7 +22,7 @@ public function dashboard()
2122
public function threads()
2223
{
2324
return view('user.threads', [
24-
'user' => $user = Auth::user(),
25+
'user' => $user = User::scopes('withCounts')->find(Auth::id()),
2526
'threads' => $user->threads()
2627
->recent()
2728
->paginate(5),
@@ -31,7 +32,7 @@ public function threads()
3132
public function discussions()
3233
{
3334
return view('user.discussions', [
34-
'user' => $user = Auth::user(),
35+
'user' => $user = User::scopes('withCounts')->find(Auth::id()),
3536
'discussions' => $user->discussions()
3637
->orderByDesc('created_at')
3738
->paginate(5),

app/Models/User.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,27 +310,27 @@ public function replies(): Collection
310310

311311
public function countReplies(): int
312312
{
313-
return Cache::remember('replies_count', now()->addHours(2), fn () => $this->replyAble()->count());
313+
return $this->replyAble()->count();
314314
}
315315

316316
public function countSolutions(): int
317317
{
318-
return Cache::remember('solutions_count', now()->addHours(2), fn () => $this->replyAble()->isSolution()->count());
318+
return $this->replyAble()->isSolution()->count();
319319
}
320320

321321
public function countArticles(): int
322322
{
323-
return Cache::remember('articles_count', now()->addHours(2), fn () => $this->articles()->approved()->count());
323+
return $this->articles()->approved()->count();
324324
}
325325

326326
public function countDiscussions(): int
327327
{
328-
return Cache::remember('discussions_count', now()->addHours(2), fn () => $this->discussions()->count());
328+
return $this->discussions()->count();
329329
}
330330

331331
public function countThreads(): int
332332
{
333-
return Cache::remember('threads_count', now()->addHours(2), fn () => $this->threads()->count());
333+
return $this->threads()->count();
334334
}
335335

336336
public function scopeMostSolutions(Builder $query, int $inLastDays = null)
@@ -371,12 +371,13 @@ public function scopeMostSubmissionsInLastDays(Builder $query, int $days)
371371
public function scopeWithCounts(Builder $query)
372372
{
373373
return $query->withCount([
374+
'discussions as discussions_count',
374375
'articles as articles_count',
375376
'threads as threads_count',
376377
'replyAble as replies_count',
377378
'replyAble as solutions_count' => function (Builder $query) {
378379
return $query->join('threads', 'threads.solution_reply_id', '=', 'replies.id')
379-
->where('replyable_type', Thread::class);
380+
->where('replyable_type', 'thread');
380381
},
381382
]);
382383
}

0 commit comments

Comments
 (0)