In Laravel 11, the kernel has been refactored, and the configuration of middleware groups has moved. Instead of modifying the $middlewareGroups property in the Kernel.php file, you now need to register your middleware in the app/bootstrap/app.php file using the MiddlewarePriority class.
Here's how you can add the Spatie MultiTenancy middleware to the 'web' middleware group in Laravel 11:
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Http\Middleware\MiddlewarePriority;
use Spatie\Multitenancy\Http\Middleware\NeedsTenant;
use Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession;
$app = new Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
// ... other bootstrap code ...
// Register the Spatie MultiTenancy middleware in the 'web' group
MiddlewarePriority::forWeb([
// ... other web middleware ...
NeedsTenant::class,
EnsureValidTenantSession::class,
]);
// ... rest of the bootstrap/app.php file ...
return $app;
Make sure to place the MiddlewarePriority::forWeb([...]) call at the appropriate place in your bootstrap/app.php file, where other middleware groups are being registered or after all other bootstrap code has been executed.
This should correctly register the Spatie MultiTenancy middleware in the 'web' middleware group, and you should be able to proceed with the installation of the Spatie MultiTenancy package as intended.