@@ -238,6 +238,8 @@ public function syncRoles(...$roles)
238
238
*/
239
239
public function hasRole ($ roles , ?string $ guard = null ): bool
240
240
{
241
+ $ roleKey = $ this ->getRoleKey ();
242
+
241
243
$ this ->loadMissing ('roles ' );
242
244
243
245
if (is_string ($ roles ) && strpos ($ roles , '| ' ) !== false ) {
@@ -249,7 +251,7 @@ public function hasRole($roles, ?string $guard = null): bool
249
251
250
252
return $ this ->roles
251
253
->when ($ guard , fn ($ q ) => $ q ->where ('guard_name ' , $ guard ))
252
- ->pluck (' name ' )
254
+ ->pluck ($ roleKey )
253
255
->contains (function ($ name ) use ($ roles ) {
254
256
/** @var string|\BackedEnum $name */
255
257
if ($ name instanceof \BackedEnum) {
@@ -270,8 +272,8 @@ public function hasRole($roles, ?string $guard = null): bool
270
272
271
273
if (is_string ($ roles )) {
272
274
return $ guard
273
- ? $ this ->roles ->where ('guard_name ' , $ guard )->contains (' name ' , $ roles )
274
- : $ this ->roles ->contains (' name ' , $ roles );
275
+ ? $ this ->roles ->where ('guard_name ' , $ guard )->contains ($ roleKey , $ roles )
276
+ : $ this ->roles ->contains ($ roleKey , $ roles );
275
277
}
276
278
277
279
if ($ roles instanceof Role) {
@@ -314,6 +316,8 @@ public function hasAnyRole(...$roles): bool
314
316
*/
315
317
public function hasAllRoles ($ roles , ?string $ guard = null ): bool
316
318
{
319
+ $ roleKey = $ this ->getRoleKey ();
320
+
317
321
$ this ->loadMissing ('roles ' );
318
322
319
323
if ($ roles instanceof \BackedEnum) {
@@ -332,16 +336,16 @@ public function hasAllRoles($roles, ?string $guard = null): bool
332
336
return $ this ->roles ->contains ($ roles ->getKeyName (), $ roles ->getKey ());
333
337
}
334
338
335
- $ roles = collect ()->make ($ roles )->map (function ($ role ) {
339
+ $ roles = collect ()->make ($ roles )->map (function ($ role ) use ( $ roleKey ) {
336
340
if ($ role instanceof \BackedEnum) {
337
341
return $ role ->value ;
338
342
}
339
343
340
- return $ role instanceof Role ? $ role ->name : $ role ;
344
+ return $ role instanceof Role ? $ role ->{ $ roleKey } : $ role ;
341
345
});
342
346
343
347
$ roleNames = $ guard
344
- ? $ this ->roles ->where ('guard_name ' , $ guard )->pluck (' name ' )
348
+ ? $ this ->roles ->where ('guard_name ' , $ guard )->pluck ($ roleKey )
345
349
: $ this ->getRoleNames ();
346
350
347
351
$ roleNames = $ roleNames ->transform (function ($ roleName ) {
@@ -362,6 +366,8 @@ public function hasAllRoles($roles, ?string $guard = null): bool
362
366
*/
363
367
public function hasExactRoles ($ roles , ?string $ guard = null ): bool
364
368
{
369
+ $ roleKey = $ this ->getRoleKey ();
370
+
365
371
$ this ->loadMissing ('roles ' );
366
372
367
373
if (is_string ($ roles ) && strpos ($ roles , '| ' ) !== false ) {
@@ -373,10 +379,10 @@ public function hasExactRoles($roles, ?string $guard = null): bool
373
379
}
374
380
375
381
if ($ roles instanceof Role) {
376
- $ roles = [$ roles ->name ];
382
+ $ roles = [$ roles ->{ $ roleKey } ];
377
383
}
378
384
379
- $ roles = collect ()->make ($ roles )->map (fn ($ role ) => $ role instanceof Role ? $ role ->name : $ role
385
+ $ roles = collect ()->make ($ roles )->map (fn ($ role ) => $ role instanceof Role ? $ role ->{ $ roleKey } : $ role
380
386
);
381
387
382
388
return $ this ->roles ->count () == $ roles ->count () && $ this ->hasAllRoles ($ roles , $ guard );
@@ -390,11 +396,17 @@ public function getDirectPermissions(): Collection
390
396
return $ this ->permissions ;
391
397
}
392
398
399
+ /**
400
+ * Return all role identifiers for the model.
401
+ *
402
+ * Note: This respects the configured 'role_identifier' column (e.g., 'name' or 'slug'),
403
+ * even though the method name is 'getRoleNames' for backward compatibility.
404
+ */
393
405
public function getRoleNames (): Collection
394
406
{
395
407
$ this ->loadMissing ('roles ' );
396
408
397
- return $ this ->roles ->pluck (' name ' );
409
+ return $ this ->roles ->pluck ($ this -> getRoleKey () );
398
410
}
399
411
400
412
protected function getStoredRole ($ role ): Role
@@ -408,7 +420,7 @@ protected function getStoredRole($role): Role
408
420
}
409
421
410
422
if (is_string ($ role )) {
411
- return $ this ->getRoleClass ()::findByName ($ role , $ this ->getDefaultGuardName ());
423
+ return $ this ->getRoleClass ()::findByKey ($ role , $ this ->getDefaultGuardName ());
412
424
}
413
425
414
426
return $ role ;
@@ -435,4 +447,10 @@ protected function convertPipeToArray(string $pipeString)
435
447
436
448
return explode ('| ' , trim ($ pipeString , $ quoteCharacter ));
437
449
}
450
+
451
+ protected function getRoleKey (): string
452
+ {
453
+ return config ('permission.role_identifier ' , 'name ' );
454
+ }
455
+
438
456
}
0 commit comments