Skip to content

Commit 50c8ce1

Browse files
committed
Added orderByFollowersCount*() methods.
1 parent 5fb2b71 commit 50c8ce1

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ $user->attachFollowStatus($users);
207207
```
208208

209209

210+
### Order by followers count
211+
212+
You can query users order by followers count with following methods:
213+
214+
- `orderByFollowersCountDesc()`
215+
- `orderByFollowersCountAsc()`
216+
- `orderByFollowersCount(string $direction = 'desc')`
217+
218+
example:
219+
220+
```php
221+
$users = User::orderByFollowersCountDesc()->get();
222+
$mostPopularUser = User::orderByFollowersCountDesc()->first();
223+
```
210224

211225
### N+1 issue
212226

src/Followable.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ public function areFollowingEachOther($user): bool
146146
return $this->isFollowing($user) && $this->isFollowedBy($user);
147147
}
148148

149+
public function scopeOrderByFollowersCount($query, string $direction = 'desc')
150+
{
151+
return $query->withCount('followers')->orderBy('followers_count', $direction);
152+
}
153+
154+
public function scopeOrderByFollowersCountDesc($query)
155+
{
156+
return $this->scopeOrderByFollowersCount($query, 'desc');
157+
}
158+
159+
public function scopeOrderByFollowersCountAsc($query)
160+
{
161+
return $this->scopeOrderByFollowersCount($query, 'asc');
162+
}
163+
149164
public function followers(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
150165
{
151166
/* @var \Illuminate\Database\Eloquent\Model $this */

tests/FeatureTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,53 @@ function () use ($user1, $users) {
216216

217217

218218
// with custom resolver
219-
$posts = \collect(['author' => $user2], ['author' => $user3], ['author' => $user4]);
220-
$user1->attachFollowStatus($posts, fn ($post) => $post['author']);
219+
$users = \collect(['creator' => $user2], ['creator' => $user3], ['creator' => $user4]);
220+
$user1->attachFollowStatus($users, fn ($post) => $post['creator']);
221+
}
222+
223+
public function test_order_by_followers()
224+
{
225+
/* @var \Tests\User $user1 */
226+
/* @var \Tests\User $user2 */
227+
/* @var \Tests\User $user3 */
228+
/* @var \Tests\User $user4 */
229+
/* @var \Tests\User $user5 */
230+
$user1 = User::create(['name' => 'user1']);
231+
$user2 = User::create(['name' => 'user2']);
232+
$user3 = User::create(['name' => 'user3']);
233+
$user4 = User::create(['name' => 'user4']);
234+
$user5 = User::create(['name' => 'user5']);
235+
236+
// user2: 2 followers
237+
$user1->follow($user2);
238+
$user3->follow($user2);
239+
240+
// user3: 0 followers
241+
// user4: 1 followers
242+
$user1->follow($user4);
243+
244+
// user1: 3 followers
245+
$user2->follow($user1);
246+
$user3->follow($user1);
247+
$user4->follow($user1);
248+
249+
$usersOrderByFollowersCount = User::orderByFollowersCountDesc()->get();
250+
// same as:
251+
// $usersOrderByFollowersCount = User::withCount('followers')->orderByDesc('followers_count')->get();
252+
253+
$this->assertSame($user1->name, $usersOrderByFollowersCount[0]->name);
254+
$this->assertEquals(3, $usersOrderByFollowersCount[0]->followers_count);
255+
$this->assertSame($user2->name, $usersOrderByFollowersCount[1]->name);
256+
$this->assertEquals(2, $usersOrderByFollowersCount[1]->followers_count);
257+
$this->assertSame($user4->name, $usersOrderByFollowersCount[2]->name);
258+
$this->assertEquals(1, $usersOrderByFollowersCount[2]->followers_count);
259+
$this->assertSame($user3->name, $usersOrderByFollowersCount[3]->name);
260+
$this->assertEquals(0, $usersOrderByFollowersCount[3]->followers_count);
261+
262+
$mostPopularUser = User::orderByFollowersCountDesc()->first();
263+
// same as:
264+
// $mostPopularUser = Post::withCount('followers')->orderByDesc('followers_count')->first();
265+
$this->assertSame($user1->name, $mostPopularUser->name);
266+
$this->assertEquals(3, $mostPopularUser->followers_count);
221267
}
222268
}

0 commit comments

Comments
 (0)