Skip to content

Commit

Permalink
Fix can't save the same model twice
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Apr 19, 2024
1 parent ce67a8b commit 8b0fb75
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Traits/HasPermissions.php
Expand Up @@ -404,7 +404,7 @@ public function givePermissionTo(...$permissions)

$class::saved(
function ($object) use ($permissions, $model, $teamPivot) {
if ($model->getKey() != $object->getKey()) {
if (!$object->wasRecentlyCreated || $model->getKey() != $object->getKey()) {
return;
}
$model->permissions()->attach($permissions, $teamPivot);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasRoles.php
Expand Up @@ -162,7 +162,7 @@ public function assignRole(...$roles)

$class::saved(
function ($object) use ($roles, $model, $teamPivot) {
if ($model->getKey() != $object->getKey()) {
if (!$object->wasRecentlyCreated || $model->getKey() != $object->getKey()) {
return;
}
$model->roles()->attach($roles, $teamPivot);
Expand Down
2 changes: 2 additions & 0 deletions tests/HasPermissionsTest.php
Expand Up @@ -636,6 +636,8 @@ public function it_can_sync_permissions_to_a_model_that_is_not_persisted()
$user = new User(['email' => 'test@user.com']);
$user->syncPermissions('edit-articles');
$user->save();
$user = $user->fresh();
$user->save();

$this->assertTrue($user->hasPermissionTo('edit-articles'));

Expand Down
2 changes: 2 additions & 0 deletions tests/HasRolesTest.php
Expand Up @@ -341,6 +341,8 @@ public function it_will_sync_roles_to_a_model_that_is_not_persisted()
$user = new User(['email' => 'test@user.com']);
$user->syncRoles([$this->testUserRole]);
$user->save();
$user = $user->fresh();
$user->save();

$this->assertTrue($user->hasRole($this->testUserRole));

Expand Down

0 comments on commit 8b0fb75

Please sign in to comment.