my code:
$user_id=1;
$user = User::find($user_id);
$roles = $user->getRoleNames();
$roles->map(function ($role) use ($user) {
$user->removeRole($role);
});
auth file
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
roles table

model_has_roles table

Why I add to User Model
Protected $guard_name ='api';
Just fine

auth.php
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
], 锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵
I'm not sure I understand your post.
OP means, that HasRoles doesn't use default guard from config/auth.php.
I have in config/auth.php:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
]
and seeder:
$role = Role::create(['name' => 'super-admin']);
$role->givePermissionTo(Permission::all());
The seeder will add "super-admin" role to api guard.
While using $user->assignRole('super-admin'); is throwed error:
There is no role named
super-admin
and need add protected $guard_name = 'api'; to model, to fix it.
https://github.com/spatie/laravel-permission/blob/master/src/Guard.php#L49
static::getNames($class)->first() returns "web" despite default guard set on "api". Probably User model have guard defaults set on 'web'.
I had the same issue. Spent 1 hour wondering why it wouldn't find the role.
Like written, defining guard_name on model solves it. But this shouldn't be necessary.
@canvural wrote:
But this shouldn't be necessary.
I understand, and agree. We're rewriting a number of things for (what might be v3) and aim to improve this... particularly the way it handles guards. (Right now it looks to the model as the primary source of truth, and if not set there then falls back to other options based on configs.)
This is a bad idea:
$role = Role::create(['name' => 'super-admin']);
$role->givePermissionTo(Permission::all());
Because when new permissions are added, super-admin will not have perm.
It is much better to do this through Laravel Gate:
Gate::before(function (User $user) {
if ($user->hasRole('super-admin')) {
return true;
}
});
True, this is better: https://docs.spatie.be/laravel-permission/v2/basic-usage/super-admin/
@drbyte I am still experiencing this issue in v3 despite setting default guard in my config to api. I still need to add $guard_name to my User model.
Is this intended?
@danvim wrote:
@drbyte I am still experiencing this issue in v3 despite setting default guard in my config to api. I still need to add
$guard_nameto myUsermodel.Is this intended?
Yes. v3 didn't get those sweeping changes. They haven't been completed yet.
For compatibility with Laravel's semantic numbering change, and other dependencies, we released v3 as basically v2 but with modern dependencies. This also allowed us to remove code required to patch for much older Laravel and PHP versions.
Apologies for any confusion. Numbering is sometimes hard :)
@drbyte Right. I was just confused since I saw the issue was closed and thought it has been fixed and released. If the issue is tracked somewhere else, that's fine. It's working with $guard_name anyway.
@SzymonLisowiec
I had another guard alongsite api and web.
As you followed to add $guard_name = "web" property in User class, It work with web guard but what about another guard (here my guard name is *customer)?*
customer guard does not work.
@SzymonLisowiec
I had another guard alongsite api and web.
As you followed to add$guard_name = "web"property in User class, It work with web guard but what about another guard (here my guard name is *customer)?*
customer guard does not work.
Check it: https://docs.spatie.be/laravel-permission/v3/basic-usage/multiple-guards/
I got a solution for this.
public static $guard_name = "customer" property in User model.$guard_name property like thisUser::$guard_name = 'guard name';$user->assignRole("role name");
Most helpful comment
Why I add to User Model
Protected $guard_name ='api';
Just fine
auth.php
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
], 锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵