4

I'm using Laravel-Permissions and wrote in rotes\web.php:

Route::group(['middleware' => ['role:admin']], function () {
    Route::get('/admin', function () {
        return "Test";
    });
});

I added in app\Http\Kernel.php :

protected $routeMiddleware = [
    // ...
    'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
    'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
    'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
];

Error:

enter image description here

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
Andrew Stetsko
  • 87
  • 1
  • 1
  • 9

3 Answers3

6

You forgot to include the roles trait

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles; //this line

    // ...
}
N69S
  • 16,110
  • 3
  • 22
  • 36
  • 1
    I have got same error Call to undefined method App\\Models\\User::roles(), in laravel nova 4 with spatie/laravel-permission model, this is the correct answer. thanks – kosala manojeewa Oct 03 '22 at 18:07
1

Make sure your User model uses the Spatie\Permission\Traits\HasRoles trait.

<?php

use Spatie\Permission\Traits\HasRoles;

class User
{
    use HasRoles;
}
jrcamatog
  • 1,341
  • 5
  • 14
0

i think you did not add this :

use Spatie\Permission\Traits\HasRoles;

class User
{
    use HasRoles;
}