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

shahmal1yev's avatar

Laravel session is not working correctly

session(['locale' => 'az']);

The above returns "az" when page loaded (session("locale"))

session(['foo' => 'bar'])

The above returns null when page loaded. What is the reason of this?

0 likes
9 replies
jlrdw's avatar

What code are you using to set session(['foo' => 'bar'])

Snapey's avatar

if you consider that there is no difference between locale and foo, and az and bar then your question makes no sense

shahmal1yev's avatar

I was adding the value via API request. But session is starts in the web array in the kernel. I moved the \Illuminate\Session\Middleware\StartSession::class to $middleware. It working now

tykus's avatar

I was adding the value via API request

I moved the \Illuminate\Session\Middleware\StartSession::class to $middleware

So every API request gets some Session !🕺🕺🕺

Or, maybe you need to ask yourself why web and api get different middlewares?

1 like
shahmal1yev's avatar

If i not do that not get value of "locale" from session in exception handler for error views

tykus's avatar

Whatever API endpoint is setting the locale should not be an API endpoint; move it into the web routes.

1 like
automica's avatar

Api should be stateless. If you need seasons move your route to web.php

shahmal1yev's avatar

If i not do that not get value of "locale" from session in exception handler for error views. Do I have to store the active language in cookies?

Please or to participate in this conversation.