Skip to content

Commit d8e687c

Browse files
committed
Improve readme description of how defaults work with multiple guards
1 parent 7762045 commit d8e687c

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -540,25 +540,16 @@ However when using multiple guards they will act like namespaces for your permis
540540

541541
### Using permissions and roles with multiple guards
542542

543-
By default the default guard (`config('auth.defaults.guard')`) will be used as the guard for new permissions and roles. When creating permissions and roles for specific guards you'll have to specify their `guard_name` on the model:
543+
When creating new permissions and roles, if no guard is specified, then the **first** defined guard in `auth.guards` config array will be used. When creating permissions and roles for specific guards you'll have to specify their `guard_name` on the model:
544544

545545
```php
546546
// Create a superadmin role for the admin users
547-
$role = Role::create(['guard_name' => 'admin', 'name' => 'superadmin']);
548547

549-
// Define a `publish articles` permission for the admin users belonging to the admin guard
550-
$permission = Permission::create(['guard_name' => 'admin', 'name' => 'publish articles']);
551-
552-
// Define a *different* `publish articles` permission for the regular users belonging to the web guard
553-
$permission = Permission::create(['guard_name' => 'web', 'name' => 'publish articles']);
554-
```
555-
556-
To check if a user has permission for a specific guard:
557-
558-
```php
559548
$user->hasPermissionTo('publish articles', 'admin');
560549
```
561550

551+
> **Note**: When determining whether a role/permission is valid on a given model, it chooses the guard in this order: first the `$guard_name` property of the model; then the guard in the config (through a provider); then the first-defined guard in the `auth.guards` config array; then the `auth.defaults.guard` config.
552+
562553
### Assigning permissions and roles to guard users
563554

564555
You can use the same methods to assign permissions and roles to users as described above in [using permissions via roles](#using-permissions-via-roles). Just make sure the `guard_name` on the permission or role matches the guard of the user, otherwise a `GuardDoesNotMatch` exception will be thrown.

0 commit comments

Comments
 (0)