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

dgvai's avatar
Level 1

Laravel Session behaving weird!

In short, I'm trying to make a language switcher by user using session. So what I am doing:

From my controller

session()->put('locale',$request->locale);  // here $request->locale = 'en' or 'bn' in switching cases
return redirect()->back();

Created an Middleware named App, so: From my middleware


public function handle($request, Closure $next)
    {
        if(session()->has('locale') == false)
        {
            session()->put('locale',config('app.locale'));
        }
        app()->setLocale(session()->get('locale'));
        return $next($request);
    }

also added the middleware to the Kernel.php, no problem there.

Problem is here:

from my blade template, if I say {{session()->get('locale')}} it says either 'en' or 'bn' according to the work done by the controller.

But in the middleware, if I dd(session()->has('locale') ) it always says false, thus it is always setting app locale to the default config files configuration.

What can be the problem? How can I solve this?

I am using, Laravel 7

0 likes
1 reply
dgvai's avatar
dgvai
OP
Best Answer
Level 1

Figured out the solution!

setting the middleware in the web middleware group!

Please or to participate in this conversation.