@@ -39,6 +39,9 @@ final class Index extends Component
39
39
#[Url(as: 'follow ' )]
40
40
public ?string $ subscribe = null ;
41
41
42
+ #[Url(as: 'popular ' )]
43
+ public ?string $ popular = null ;
44
+
42
45
public ?Channel $ currentChannel = null ;
43
46
44
47
public string $ search = '' ;
@@ -54,6 +57,18 @@ public function mount(): void
54
57
$ this ->locale = config ('app.locale ' );
55
58
}
56
59
60
+ protected function applyPopular (Builder $ query ): Builder
61
+ {
62
+ if ($ this ->popular ) {
63
+ return $ query
64
+ ->withCount ('replies ' )
65
+ ->orderByDesc ('replies_count ' )
66
+ ->OrderByViews (); // @phpstan-ignore-line
67
+ }
68
+
69
+ return $ query ;
70
+ }
71
+
57
72
#[On('channelUpdated ' )]
58
73
public function reloadThreads (?int $ channelId ): void
59
74
{
@@ -145,8 +160,7 @@ protected function applyUnAnswer(Builder $query): Builder
145
160
146
161
public function render (): View
147
162
{
148
- $ query = Thread::with (['channels ' , 'user ' ])
149
- ->orderByDesc ('created_at ' );
163
+ $ query = Thread::with (['channels ' , 'user ' ]);
150
164
151
165
$ query = $ this ->applyChannel ($ query );
152
166
$ query = $ this ->applySearch ($ query );
@@ -155,6 +169,7 @@ public function render(): View
155
169
$ query = $ this ->applyAuthor ($ query );
156
170
$ query = $ this ->applySubscribe ($ query );
157
171
$ query = $ this ->applyUnAnswer ($ query );
172
+ $ query = $ this ->applySorting ($ query );
158
173
159
174
$ threads = $ query
160
175
->scopes ('withViewsCount ' )
@@ -165,4 +180,11 @@ public function render(): View
165
180
])
166
181
->title (__ ('pages/forum.channel_title ' , ['channel ' => isset ($ this ->currentChannel ) ? ' ~ ' .$ this ->currentChannel ->name : '' ]));
167
182
}
183
+
184
+ private function applySorting (Builder $ query ): Builder
185
+ {
186
+ return $ this ->popular
187
+ ? $ this ->applyPopular ($ query )
188
+ : $ query ->orderByDesc ('created_at ' );
189
+ }
168
190
}
0 commit comments