Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

leostereo's avatar

Can not store session value.

Hi guys , im running laravel 5.8. Im trying to set and get some session values but I got following message:

 RuntimeException
Session store not set on request.

This is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SessionController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
   public function storeSessionData(Request $request) {
        $value = $request->session()->put('key');

        //
    }
}

I did not tweak anything else.

Any ideas ? Leandro.

0 likes
4 replies
Ksandar's avatar
Ksandar
Best Answer
Level 26

@leostereo it looks like it's not a web route (specified not in web.php), so the session was not initialized. Or StartSession middleware was not included for this route.

Check your app/Http/Kernel.php class

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        // ...
        \Illuminate\Session\Middleware\StartSession::class, // <- this middleware is responsible for initializing sessions
        // ...
    ],

    'api' => [
        'throttle:60,1',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];
leostereo's avatar

Thanks !!! It worked , I was working with api routes , so doing:

   protected $middlewareGroups = [
       'web' => [
           \App\Http\Middleware\EncryptCookies::class,
           \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
           \Illuminate\Session\Middleware\StartSession::class,
           // \Illuminate\Session\Middleware\AuthenticateSession::class,
           \Illuminate\View\Middleware\ShareErrorsFromSession::class,
           \App\Http\Middleware\VerifyCsrfToken::class,
           \Illuminate\Routing\Middleware\SubstituteBindings::class,
       ],

       'api' => [
           \Illuminate\Session\Middleware\StartSession::class,
           'throttle:60,1',
           'bindings',
       ],
   ];

btw ... looking at session documentation it does not mention anything about this line. Thankyou.

Snapey's avatar

From the docs

The api.php file contains routes that the RouteServiceProvider places in the api middleware group, which provides rate limiting. These routes are intended to be stateless, so requests entering the application through these routes are intended to be authenticated via tokens and will not have access to session state.

Please or to participate in this conversation.