Session Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

Session Store Not Set on Request in Laravel - Fix

This error occurs when trying to access session data before the session middleware has initialized.

The Error

Error Message
Session store not set on request

Common Causes

  1. 1 Route not using web middleware group
  2. 2 Accessing session in service provider boot
  3. 3 API route trying to use session
  4. 4 Custom middleware running before session starts

Solutions

1

Ensure route uses web middleware

PHP
// In routes/web.php (has web middleware by default)
Route::get('/dashboard', [DashboardController::class, 'index']);

// Or explicitly add middleware
Route::middleware(['web'])->group(function () {
    Route::get('/profile', [ProfileController::class, 'show']);
});
2

For API routes needing sessions

PHP
// If you need sessions in API routes
// In app/Http/Kernel.php, add to 'api' middleware group:
'api' => [
    \Illuminate\Session\Middleware\StartSession::class,
    // ... other middleware
],
3

Don't access session in service providers

PHP
// Wrong - session not available in boot()
public function boot()
{
    $user = session('user'); // Error!
}

// Right - use middleware or controller instead
// Or defer using a callback
$this->app->booted(function () {
    // Session available here after full boot
});
4

Check middleware order

PHP
// In Kernel.php, ensure StartSession is early
protected $middlewareGroups = [
    'web' => [
        \Illuminate\Cookie\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class, // Must be before auth
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        // ...
    ],
];

Need Help With Your Laravel Project?

I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.

Currently available for 2-3 new projects

Hafiz Riaz

About Hafiz

Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.

View Portfolio

Related Errors