From a0ccba48dc408c4c1902cd8abec0c1e61a242165 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Thu, 24 Oct 2024 15:55:15 +0200 Subject: [PATCH] Update role stats handling --- app/Models/RoleStat.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Models/RoleStat.php b/app/Models/RoleStat.php index cab474d23..acd6e14f2 100644 --- a/app/Models/RoleStat.php +++ b/app/Models/RoleStat.php @@ -16,7 +16,12 @@ class RoleStat extends Model { $roles = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderByDesc('users_count')->get(); foreach ($roles as $role) { - self::updateOrCreate(['role' => $role->name, 'users' => $role->users_count]); + // Check if we already have the information and if we do just update the count + if (self::query()->where('role', $role->name)->exists()) { + self::query()->where('role', $role->name)->update(['users' => $role->users_count]); + continue; + } + self::query()->create(['role' => $role->name, 'users' => $role->users_count]); } }