Target class [Spatie\Permission\Middlewares\PermissionMiddleware] does not exist

1.7k views Asked by At

I hope you’re all right. I was wondering if you could take a look at a problem I’ve encountered in a Laravel project that uses Spatie Laravel Permission.

The mistake I’m facing is this:

Target class [Spatie\Permission\Middlewares\PermissionMiddleware] does not exist.

I have reviewed the documentation and tried some common solutions, but so far I have had no luck in finding a solution.

I very much appreciate any suggestions or ideas you may have.

Thanks in advance for your help!

spatie/laravel-permission 6.0 Laravel Framework 10.29.0 PHP 8.2.11 laravel/jetstream 4.0 livewire/livewire 3.0

2

There are 2 answers

1
Wilz On BEST ANSWER

Note: Make Middleware instead of Middlewares in the Path

I was also facing the same issue when I install this package but I found a Solution that could may help you

In the documentation there could be like in the kernal.php file

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

You can use this in the kernal.php file

protected $routeMiddleware = [
    'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
    'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
    'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
];
0
Arif I. On

I had the same issue when I upgraded Spatie's package laravel-permission from version 3 to 6. I followed the v6 upgrade guide and found the solution.

The guide says that the \Spatie\Permission\Middlewares\ namespace has been changed to \Spatie\Permission\Middleware\ (singular). So, I had to update any references to the middleware classes in my /app/Http/Kernel.php.

Here is how my /app/Http/Kernel.php file looked before and after the update:

Before: (Notice Middlewares being plural)

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

After: (Notice Middleware being singular)

'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,

This fixed the issue for me. I hope this helps you too.