I am using Laravel 5.6 with spatie/laravel-permission version 2.9 also using Laravel Passport as auth driver with $guard = 'api'.
When I am trying to assign an array of permission like ['edit_project', 'add_project' 'delete_project'] to a role with help of this function
public function assignPermissions($role, $permissions)
{
$role = Role::findByName($role);
$role->givePermissionTo($permissions);
return $role;
}
but getting the error There is no permission namededit_projectfor guardapi.`
Also I have at config/auth.php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
What is the code you used to create the permissions you are attempting to assign? (permissions must be created before they can be assigned to a role)
@drbyte I am seeding the permission table by help of Larvel seeder which means I have predefined permissions in my system as well as roles I have attached my permission table which looks at the first time when I am seeding it like below that the guard_name is web.

but manually I am changing theguard_name field to api which my permission table became like this.

After you make the manual edits to the database content, are you clearing the laravel cache?
I have a similliar problem.
What I want to achieve is in case I have two permissions, one belongs to api guard and the other to web guard, then I want to give the User both permissions, how can I achieve that?
@gregoriochiko you mentioned permissions and didn't say anything about roles, so I'll assume your'e only interested in specific permissions.
This also assumes you want to assign permissions for all guards, and have already created separate permissions for all guards.
// pick a permission name
$permission_name = 'whatever';
// lookup all defined permissions, regardless of guard
$permissions = app(PermissionRegistrar::class)->getPermissions()
// filter down to just those matching the specified name
->filter(function ($permission) use ($permission_name) {
return $permission->name === $permission_name;
});
// grant those specific permissions to the user
$user->givePermissionTo($permissions);
I've fixed this by creating another model that extends User model, so that new model can have api guard while User still have the default guard (web).
i m also facing this problem that it works web guard but not with api so help us how to fix this by api guard too
Thanks