@@ -45,15 +45,19 @@ public static function create(array $attributes = [])
4545 $ attributes ['guard_name ' ] ??= Guard::getDefaultName (static ::class);
4646
4747 $ params = ['name ' => $ attributes ['name ' ], 'guard_name ' => $ attributes ['guard_name ' ]];
48- if (app (PermissionRegistrar::class)->teams ) {
49- $ teamsKey = app (PermissionRegistrar::class)->teamsKey ;
48+
49+ $ registrar = app (PermissionRegistrar::class);
50+
51+ if ($ registrar ->teams ) {
52+ $ teamsKey = $ registrar ->teamsKey ;
5053
5154 if (array_key_exists ($ teamsKey , $ attributes )) {
5255 $ params [$ teamsKey ] = $ attributes [$ teamsKey ];
5356 } else {
5457 $ attributes [$ teamsKey ] = getPermissionsTeamId ();
5558 }
5659 }
60+
5761 if (static ::findByParam ($ params )) {
5862 throw RoleAlreadyExists::create ($ attributes ['name ' ], $ attributes ['guard_name ' ]);
5963 }
@@ -66,11 +70,13 @@ public static function create(array $attributes = [])
6670 */
6771 public function permissions (): BelongsToMany
6872 {
73+ $ registrar = app (PermissionRegistrar::class);
74+
6975 return $ this ->belongsToMany (
7076 config ('permission.models.permission ' ),
7177 config ('permission.table_names.role_has_permissions ' ),
72- app (PermissionRegistrar::class) ->pivotRole ,
73- app (PermissionRegistrar::class) ->pivotPermission
78+ $ registrar ->pivotRole ,
79+ $ registrar ->pivotPermission
7480 );
7581 }
7682
@@ -135,10 +141,18 @@ public static function findOrCreate(string $name, ?string $guardName = null): Ro
135141 {
136142 $ guardName ??= Guard::getDefaultName (static ::class);
137143
138- $ role = static ::findByParam (['name ' => $ name , 'guard_name ' => $ guardName ]);
144+ $ attributes = ['name ' => $ name , 'guard_name ' => $ guardName ];
145+
146+ $ role = static ::findByParam ($ attributes );
139147
140148 if (! $ role ) {
141- return static ::query ()->create (['name ' => $ name , 'guard_name ' => $ guardName ] + (app (PermissionRegistrar::class)->teams ? [app (PermissionRegistrar::class)->teamsKey => getPermissionsTeamId ()] : []));
149+ $ registrar = app (PermissionRegistrar::class);
150+ if ($ registrar ->teams ) {
151+ $ teamsKey = $ registrar ->teamsKey ;
152+ $ attributes [$ teamsKey ] = getPermissionsTeamId ();
153+ }
154+
155+ return static ::query ()->create ($ attributes );
142156 }
143157
144158 return $ role ;
@@ -153,8 +167,10 @@ protected static function findByParam(array $params = []): ?RoleContract
153167 {
154168 $ query = static ::query ();
155169
156- if (app (PermissionRegistrar::class)->teams ) {
157- $ teamsKey = app (PermissionRegistrar::class)->teamsKey ;
170+ $ registrar = app (PermissionRegistrar::class);
171+
172+ if ($ registrar ->teams ) {
173+ $ teamsKey = $ registrar ->teamsKey ;
158174
159175 $ query ->where (fn ($ q ) => $ q ->whereNull ($ teamsKey )
160176 ->orWhere ($ teamsKey , $ params [$ teamsKey ] ?? getPermissionsTeamId ())
0 commit comments