Repository: spatie/laravel-permission (php)

Issue:
On Teams when delete a user without soft delete trait, not all data gets detached
**Describe the bug**
On Teams when delete a user without soft delete trait, not all data gets detached

**Versions**
You can use `composer show` to get the version numbers of:
- spatie/laravel-permission package version: 5.5.11
- illuminate/framework package: 9.x

PHP version: 8.1

**To Reproduce**
Here is my example code and/or tests showing the problem in my app:
```php
  /** @test */
  public function it_deletes_pivot_table_entries_when_deleting_models()
  {
      $user1 = User::create(['email' => 'user2@test.com']);
      $user2 = User::create(['email' => 'user2@test.com']);

      setPermissionsTeamId(1);
      $user1->assignRole('testRole');
      $user1->givePermissionTo('edit-articles');
      $user2->assignRole('testRole');
      $user2->givePermissionTo('edit-articles');
      setPermissionsTeamId(2);
      $user1->givePermissionTo('edit-news');

      $this->assertDatabaseHas('model_has_permissions', [config('permission.column_names.model_morph_key') => $user1->id]);
      $this->assertDatabaseHas('model_has_roles', [config('permission.column_names.model_morph_key') => $user1->id]);

      $user1->delete();

      setPermissionsTeamId(1);
      $this->assertDatabaseMissing('model_has_permissions', [config('permission.column_names.model_morph_key') => $user1->id]);
      $this->assertDatabaseMissing('model_has_roles', [config('permission.column_names.model_morph_key') => $user1->id]);
      $this->assertDatabaseHas('model_has_permissions', [config('permission.column_names.model_morph_key') => $user2->id]);
      $this->assertDatabaseHas('model_has_roles', [config('permission.column_names.model_morph_key') => $user2->id]);
  }
```


**Expected behavior**
Detach all teams data



Pull request description:
Fix detaching user models on teams feature #2220
Maybe Closes #2220
So expecting comments, is there a better way?

Patch:
diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php
index 7e89ec7..8234dd6 100644
--- a/src/Traits/HasPermissions.php
+++ b/src/Traits/HasPermissions.php
@@ -27,7 +27,10 @@ trait HasPermissions
                 return;
             }
 
+            $teams = PermissionRegistrar::$teams;
+            PermissionRegistrar::$teams = false;
             $model->permissions()->detach();
+            PermissionRegistrar::$teams = $teams;
         });
     }
 
diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php
index 6e4d7e5..59faeec 100644
--- a/src/Traits/HasRoles.php
+++ b/src/Traits/HasRoles.php
@@ -24,7 +24,10 @@ trait HasRoles
                 return;
             }
 
+            $teams = PermissionRegistrar::$teams;
+            PermissionRegistrar::$teams = false;
             $model->roles()->detach();
+            PermissionRegistrar::$teams = $teams;
         });
     }
 


Test patch:
diff --git a/tests/TeamHasRolesTest.php b/tests/TeamHasRolesTest.php
index 0539319..d3f35d8 100644
--- a/tests/TeamHasRolesTest.php
+++ b/tests/TeamHasRolesTest.php
@@ -9,6 +9,32 @@ class TeamHasRolesTest extends HasRolesTest
     /** @var bool */
     protected $hasTeams = true;
 
+    /** @test */
+    public function it_deletes_pivot_table_entries_when_deleting_models()
+    {
+        $user1 = User::create(['email' => 'user2@test.com']);
+        $user2 = User::create(['email' => 'user2@test.com']);
+
+        setPermissionsTeamId(1);
+        $user1->assignRole('testRole');
+        $user1->givePermissionTo('edit-articles');
+        $user2->assignRole('testRole');
+        $user2->givePermissionTo('edit-articles');
+        setPermissionsTeamId(2);
+        $user1->givePermissionTo('edit-news');
+
+        $this->assertDatabaseHas('model_has_permissions', [config('permission.column_names.model_morph_key') => $user1->id]);
+        $this->assertDatabaseHas('model_has_roles', [config('permission.column_names.model_morph_key') => $user1->id]);
+
+        $user1->delete();
+
+        setPermissionsTeamId(1);
+        $this->assertDatabaseMissing('model_has_permissions', [config('permission.column_names.model_morph_key') => $user1->id]);
+        $this->assertDatabaseMissing('model_has_roles', [config('permission.column_names.model_morph_key') => $user1->id]);
+        $this->assertDatabaseHas('model_has_permissions', [config('permission.column_names.model_morph_key') => $user2->id]);
+        $this->assertDatabaseHas('model_has_roles', [config('permission.column_names.model_morph_key') => $user2->id]);
+    }
+
     /** @test */
     public function it_can_assign_same_and_different_roles_on_same_user_different_teams()
     {