Total Complexity | 4 |
Total Lines | 23 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
9 | class RoleStat extends Model |
||
10 | { |
||
11 | use HasFactory; |
||
|
|||
12 | |||
13 | protected $guarded = []; |
||
14 | |||
15 | public static function insertUsersByRole(): void |
||
16 | { |
||
17 | $roles = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderByDesc('users_count')->get(); |
||
18 | foreach ($roles as $role) { |
||
19 | // Check if we already have the information and if we do just update the count |
||
20 | if (self::query()->where('role', $role->name)->exists()) { |
||
21 | self::query()->where('role', $role->name)->update(['users' => $role->users_count]); |
||
22 | |||
23 | continue; |
||
24 | } |
||
25 | self::query()->create(['role' => $role->name, 'users' => $role->users_count]); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | public static function getUsersByRole(): array |
||
32 | } |
||
33 | } |
||
34 |