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

stefanwegner's avatar

#Session::put() only works once

Hi there,

I have some trouble updating a value in a current session ...

On first request "my-website.com/en/whatever" the language gets detected via url segment "en"

session(['locale' => $this->current->locale]);

This will be persisted into my session. It puts the value correctly in my session, but when i switch to another language - say 'de' - the locale value will not be updated again - EVER - even if I Session::save() afterwards ...

Hope someone can help me with that ...

(The language detection works absolutely fine - I dumped the "$this->current->locale" value and it's correct)

0 likes
13 replies
jlrdw's avatar

Are you using the new web middleware as in the docs.

stefanwegner's avatar

@jlrdw - jep - middleware is in place

@moharrum - doesn't work ...

First request: Session works Second request (when i try to update a value): value will not be persisted ...

kahriman's avatar

Do you need to put in session?


Route::get('welcome/{locale}', function ($locale) { \App::setLocale($locale); });

\App::getLocale();

jlrdw's avatar

Have you tried this way from the docs

$request->session()->put('key', 'value');
stefanwegner's avatar

It is not even possible to "forget" the 'locale' key from session ... adding another is - again - no problem ...

jlrdw's avatar

Have you tried setting that value to nothing then immediately afterwards set it to what you need to set it to. This first

$request->session()->put('key', '');
bashy's avatar

You're setting/saving the session wrongly.

kahriman's avatar

perhaps you need to clean your storage/framework/session folder (if the project still in development stage).

luisgarciaalanis's avatar
Route::group(['middleware' => ['web']], function () {
// put routes here
}

// on controller
use App;
use Session;

// try using these API's
Session::set('locale', 'fr');
App::setLocale(Session::get('locale'));


// The WEB middleware enables:
   'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],
stefanwegner's avatar
stefanwegner
OP
Best Answer
Level 7

Okay, found it ...

I bootstrapped my i18n component in the boot method of the "AppServiceProvider", moved that booting process to my BaseController and it worked ... Makes sense since the session will be only available after the router booted - with that 'web' middleware ...

Thank you guys!

Cheers

Please or to participate in this conversation.