Skip to content

Commit

Permalink
Use ->withPivot() for teamed relationships (#2679)
Browse files Browse the repository at this point in the history
* use ->withPivot for teamed relationships
* add ->withPivot() method to permissions
  • Loading branch information
juliangums authored Jun 22, 2024
1 parent b34b5a3 commit ee0817f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public function permissions(): BelongsToMany
return $relation;
}

return $relation->wherePivot(app(PermissionRegistrar::class)->teamsKey, getPermissionsTeamId());
$teamsKey = app(PermissionRegistrar::class)->teamsKey;
$relation->withPivot($teamsKey);

return $relation->wherePivot($teamsKey, getPermissionsTeamId());
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public function roles(): BelongsToMany
if (! app(PermissionRegistrar::class)->teams) {
return $relation;
}

$teamsKey = app(PermissionRegistrar::class)->teamsKey;
$relation->withPivot($teamsKey);
$teamField = config('permission.table_names.roles').'.'.$teamsKey;

$teamField = config('permission.table_names.roles').'.'.app(PermissionRegistrar::class)->teamsKey;

return $relation->wherePivot(app(PermissionRegistrar::class)->teamsKey, getPermissionsTeamId())
return $relation->wherePivot($teamsKey, getPermissionsTeamId())
->where(fn ($q) => $q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId()));
}

Expand Down

0 comments on commit ee0817f

Please sign in to comment.