| Conditions | 3 |
| Paths | 3 |
| Total Lines | 11 |
| Code Lines | 6 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 | } |
||
| 34 |