@aqeel94321 you forgot to add 'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class, to routeMiddleware array in your app/Http/Kernel.php file.
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
@Sti3bas danke sir
@Sti3bas I think, developers forgot it specify in the doc
@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.
@Mcmallen En laravel 11 se agregan en el app.php dentro de la carpeta bootstrap.
@Mcmallen me too i'm trying fo fix this problem in 2025 but i can't
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!
@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!
@sti3bas thanks I got the same issue. Forgot to add middleware on app/Http/Kernel.php
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.
@hassam perfect hassam thank u so much
Always make sure you are on the latest version
https://github.com/spatie/laravel-permission/issues/408#issuecomment-1793780950 this works for me Laravel 10
If you are using middleware like 'role:admin', you first need to register it as stated here: https://spatie.be/docs/laravel-permission/v6/basic-usage/middleware . Different versions require different paths, so find the version you are using and update!
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();
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.