From bc8570019cabc95b61e3b0cbde7e3c243bf518bd Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Mon, 21 Dec 2020 00:48:21 +0000 Subject: [PATCH] Force TeamSeeder to skip creating existing permissions TeamSeeder previously created permissions that already existed, if the migration had ran twice. This commit forces the seeder to verify if the permission it's attempting to create already exists, and if not, it creates it and assigns them at the end. THis prevents an error from appearing if these permissions were created manually. In this case, the seeder will only assign the permissions instead of creating them. --- database/seeders/TeamSeeder.php | 22 +++++++++++++++------- resources/views/layouts/app.blade.php | 4 ++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/database/seeders/TeamSeeder.php b/database/seeders/TeamSeeder.php index c44468b..ddbc45f 100755 --- a/database/seeders/TeamSeeder.php +++ b/database/seeders/TeamSeeder.php @@ -22,11 +22,13 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; +use Illuminate\Support\Collection; use Spatie\Permission\Models\Permission; use Spatie\Permission\Models\Role; class TeamSeeder extends Seeder { + /** * Run the database seeds. * @@ -54,13 +56,23 @@ class TeamSeeder extends Seeder $admin = Role::where('name', 'admin')->first(); $reviewer = Role::where('name', 'reviewer')->first(); - foreach($teamPermissions as $permission) + foreach($teamPermissions as $permission) { foreach ($teamUserPermissions as $userPermission) { - Permission::create(['name' => $permission]); - Permission::create(['name' => $userPermission]); + $permCheck = Permission::where('name', $permission)->get()->all(); + if (empty($permCheck)) + { + Permission::create(['name' => $permission]); + } + + $userPermCheck = Permission::where('name', $userPermission)->get()->all(); + + if (empty($userPermCheck)) + { + Permission::create(['name' => $userPermission]); + } } } @@ -68,9 +80,5 @@ class TeamSeeder extends Seeder $reviewer->givePermissionTo($teamUserPermissions); - - - - } } diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 197d3eb..4eaee05 100755 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -12,7 +12,7 @@ - + @@ -25,7 +25,7 @@