4

i use this package :

https://github.com/spatie/laravel-permission/tree/v2

code :

     $user=User::find(2);
    $user->assignRole('admin');

and when i assign admin role to user I'm dealing with this error

There is no role named admin.Spatie\Permission\Exceptions\RoleDoesNotExist

this is my default guard in auth.php :

    <?php

return [

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

this is my roles table :

enter image description here

this is my role_has_permission table

enter image description here

and this is my permission table :

enter image description here

apokryfos
  • 38,771
  • 9
  • 70
  • 114
ali dehqani
  • 164
  • 1
  • 3
  • 18

6 Answers6

17

just add this protected property to your user model(or whatever model you are using for assigning permissions and roles).

protected $guard_name = 'api';

TEFO
  • 1,432
  • 9
  • 23
  • 2
    This works perfectly well. A quote from the documentation "If you’re using multiple guards the guard_name attribute needs to be set as well. Read about it in the using multiple guards section of the readme". link https://docs.spatie.be/laravel-permission/v3/basic-usage/multiple-guards – blakroku Apr 19 '20 at 21:02
1

Add one of the following to your User model:

public $guard_name = 'api';

Or:

public function guardName()
{
  return 'api';
}
Popsyjunior
  • 185
  • 10
1

As a convention it's best to do configurable things in the config file.
The problem with your code is the order of arranging your guards, just re-arrange as seen below.

<?php

return [

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],


    'guards' => [
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],
 ...

Once this is done, you don’t need to add protected $guard_name = 'api'; to your User model, ensure to run php artisan config:clear

Ref: Saptie Laravel Permisions

Adedoyin Akande
  • 2,266
  • 1
  • 19
  • 21
1

friends! I found solution for this problem but first of all, i illustrate why this problem happened. As you know, Laravel read the codes from top to bottom and from left to right. Moreover when we want from laravel fresh all data by command

php artisan migrate :fresh --seed

It clears all this data and the problem begin. By other word, when assign Role command to check for new role it fails because all data is cleared before it access to those data. To avoid this we should set Role seeder class before Admin seeder class in database seeder

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
       $this->call([RoleSeeder::class, AdminSeeder::class]);
    }
}
Localhousee
  • 887
  • 1
  • 5
  • 17
brisam
  • 11
  • 1
0

Add this to your user model

use Spatie\Permission\Traits\HasRoles;

and in a user model class

use HasRoles;

Here is a reference

Complete example

Ateeq
  • 59
  • 1
  • 3
-2

This worked for me:

$role = Role::create(['guard_name' => 'admin', 'name' => 'manager']);
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Ruben
  • 1
  • Please write your answer in English, as Stack Overflow is an [English-only site](//meta.stackoverflow.com/a/297680). –  May 07 '22 at 00:10