Skip to content

Commit b825d83

Browse files
committed
refactor: Streamline case-insensitive duplicate checks in Permission and Role models by utilizing a closure for name comparison
1 parent 9fd2235 commit b825d83

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/Models/Permission.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public static function create(array $attributes = [])
5151

5252
// Check for case-insensitive duplicate
5353
$existing = static::where('guard_name', $attributes['guard_name'])
54-
->whereRaw('LOWER(name) = LOWER(?)', [$attributes['name']])
55-
->first();
54+
->get()
55+
->first(function ($item) use ($attributes) {
56+
return strtolower($item->name) === strtolower($attributes['name']);
57+
});
5658

5759
if ($existing) {
5860
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);

src/Models/Role.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public static function create(array $attributes = [])
6363
}
6464

6565
// Check for case-insensitive duplicate
66-
$query = static::where('guard_name', $attributes['guard_name'])
67-
->whereRaw('LOWER(name) = LOWER(?)', [$attributes['name']]);
66+
$query = static::where('guard_name', $attributes['guard_name']);
6867

6968
if ($registrar->teams) {
7069
$teamsKey = $registrar->teamsKey;
@@ -74,7 +73,9 @@ public static function create(array $attributes = [])
7473
});
7574
}
7675

77-
$existing = $query->first();
76+
$existing = $query->get()->first(function ($item) use ($attributes) {
77+
return strtolower($item->name) === strtolower($attributes['name']);
78+
});
7879

7980
if ($existing) {
8081
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);

0 commit comments

Comments
 (0)