diff --git a/database/migrations/2024_05_15_200656_seed_permissions_for_tags.php b/database/migrations/2024_05_15_200656_seed_permissions_for_tags.php index 599816632..91e73c86e 100644 --- a/database/migrations/2024_05_15_200656_seed_permissions_for_tags.php +++ b/database/migrations/2024_05_15_200656_seed_permissions_for_tags.php @@ -34,6 +34,8 @@ */ +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; use Illuminate\Database\Migrations\Migration; use Database\Migrations\Concerns\CanModifyPermissions; @@ -57,7 +59,15 @@ public function up(): void { - $this->createPermissions($this->permissions, $this->guards); + collect($this->guards) + ->each(function (string $guard) { + $permissions = Arr::except($this->permissions, keys: DB::table('permissions') + ->where('guard_name', $guard) + ->pluck('name') + ->all()); + + $this->createPermissions($permissions, $guard); + }); } public function down(): void diff --git a/database/migrations/Concerns/CanModifyPermissions.php b/database/migrations/Concerns/CanModifyPermissions.php index 7b0cc98f0..996e20f51 100644 --- a/database/migrations/Concerns/CanModifyPermissions.php +++ b/database/migrations/Concerns/CanModifyPermissions.php @@ -44,7 +44,7 @@ trait CanModifyPermissions /** * @param array $names The keys of the array should be the permission names and the values should be the name of the group they belong to. */ - public function createPermissions(array $names, string|array $guardName): void + public function createPermissions(array $names, string $guardName): void { $groups = DB::table('permission_groups') ->pluck('id', 'name') @@ -75,19 +75,16 @@ public function createPermissions(array $names, string|array $guardName): void ...$newGroups, ]; - collect($guardName) - ->each(function (string $guardName) use ($names, $groups): void { - DB::table('permissions') - ->insert(array_map(function (string $name, string $groupName) use ($groups, $guardName): array { - return [ - 'id' => (string) Str::orderedUuid(), - 'group_id' => $groups[$groupName], - 'guard_name' => $guardName, - 'name' => $name, - 'created_at' => now(), - ]; - }, array_keys($names), array_values($names))); - }); + DB::table('permissions') + ->insert(array_map(function (string $name, string $groupName) use ($groups, $guardName): array { + return [ + 'id' => (string) Str::orderedUuid(), + 'group_id' => $groups[$groupName], + 'guard_name' => $guardName, + 'name' => $name, + 'created_at' => now(), + ]; + }, array_keys($names), array_values($names))); } /**