Aqeel94321's avatar

Laravel Spatie error [ Target class [role] does not exist. ]

Hi! here is and error how can cofigure?

Illuminate\Contracts\Container\BindingResolutionException
Target class [role] does not exist.
http://aqeel.localhost:8000/admin/dashboard
1 like
16 replies
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@aqeel94321 you forgot to add 'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class, to routeMiddleware array in your app/Http/Kernel.php file.

28 likes
Mcmallen's avatar

@Sti3bas Hello. I have the same problem, but in Laravel 11 breeze there is no kernel.php anymore. I've done all the steps now but I'm getting an error.

2 likes
najmamalik's avatar

@Sti3bas Hi @Sti3bas,

I've already added the necessary role middleware, but the issue still persists. When I log in and get successfully redirected to the admin dashboard, I encounter the following error:

pgsql Copy Edit Target class [role] does not exist. It seems Laravel can't resolve the role middleware. I've verified that I'm using Spatie's roles and permissions package.

Any help or suggestions to resolve this would be appreciated. Thank you!

najmamalik's avatar

@Mcmallen same i am facing same issue in laravel1 2 no kernel file there i manaually create but the issue is

I've already added the necessary role middleware, but the issue still persists. When I log in and get successfully redirected to the admin dashboard, I encounter the following error:

pgsql Copy Edit Target class [role] does not exist. It seems Laravel can't resolve the role middleware. I've verified that I'm using Spatie's roles and permissions package.

Any help or suggestions to resolve this would be appreciated. Thank you!

Jaikangam's avatar

@sti3bas thanks I got the same issue. Forgot to add middleware on app/Http/Kernel.php

2 likes
hassam's avatar

Laravel 11/12

In Laravel 11/12 open /bootstrap/app.php and register them there:

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
		...
    ]);

    $middleware->alias([
        'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
        'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
        'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
    ]);
})

Add the necessary trait to your User.php model:

use HasRoles;

In Laravel 9 and 10 you can add them in app/Http/Kernel.php:

Add these two in app/Http/Kernel.php in protected $routeMiddleware = [] array.

'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
 'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,

Laravel 10

// Laravel 9 uses $routeMiddleware = [ //protected $routeMiddleware = [

// Laravel 10+ uses $middlewareAliases = [

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

YOU SHOULD ALSO set the $middlewarePriority array to include this package's middleware before the SubstituteBindings middleware, else you may get 404 Not Found responses when a 403 Not Authorized response might be expected.

5 likes
blakroku's avatar

Always make sure you are on the latest version

Trevin's avatar

this solved the error for me

use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(DIR)) ->withRouting( web: DIR.'/../routes/web.php', commands: DIR.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'role' => \Spatie\Permission\Middleware\RoleMiddleware::class, 'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class, 'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class, ]); }) ->withExceptions(function (Exceptions $exceptions) { // })->create();

shrustam's avatar

For latest Laravel versions where Kernel.php doesn't exist, you should add middleware aliases to bootstrap/app.php file like this:

->withMiddleware(function (Middleware $middleware): void { // Add aliases for the Spatie Permission package middleware $middleware->alias([ 'role' => RoleMiddleware::class, 'permission' => PermissionMiddleware::class, 'role_or_permission' => RoleOrPermissionMiddleware::class, ]); })

Please or to participate in this conversation.