session issue

Published 10 months ago by nnjeim

I am using Laravel 5.4 and i am setting the session at the controller level.

A selector change in blade submits the new value to be set in the session.

following the first request the session variable is not being overritten..

what i am missing here? any hint?

$request->session()->put('company_id', $company_id);


When you say that it submits from a change in blade, do you mean an AJAX request?


Are you using barryvdh/laravel-debugbar? It shows the sessions values.


Hi and thanks for your replies.

On selector value change I am submitting a form from blade to a route controller.

from JS event.preventDefault(); document.getElementById('set-company-id').submit();

the controller method is as below $request->session()->put('company_id', $company_id); return back();

i am printing in the blade view {{ session('company_id') }}

On the first selector change the value is set to 1 for example then on subsequent changes it remains as 1.

I tried to return the value of company_id from the controller and it was returning correctly.

So i came to the conclusion that the problem is in the session setting.

I also tried to change the session driver from database back to file.... the result remained the same.


i don't understand this:

event.preventDefault(); document.getElementById('set-company-id').submit();

how does the element with id: set-company-id, send the proper value to be updated in the session.

document.getElementById('set-company-id') whether it's a form or what not, means it must be unique. If you have multiple elements with the same id, you'll always be sending the same value. (I think it's the first one in the DOM).

am I missing something?


Hi robrogers3,

in the submitted for there is a named select element, the select action submits the form to a route controller. I already validated that the selected value is submitted correctly by returning it to the view. It is clear that the problem resides in the session setting. I have read somewhere that this problem is related to scoped routes. Any suggestion here?


Have you tried calling Session::save() after the put()? It wouldn't answer the question "why" this is happening, but it would be interesting if that did persist the session value.


Hi Talinon,

i have added Session::save() after the put, this does not seem to change the problem. i have returned the session('company_id') from the controller and it is returning correctly when i toggle between different companies.

I discovered that the issue is that the session is returning null in other controllers, models or blade views.

anyone ever encountered something similar?


I got a question what middleware are you applying to the different route groups?

do they both include the web middleware?

if they don't then thats your problem.


Hi robrogers3,

i am using scoped controllers. But all are part of web.php

i am using Auth as the group middleware but i am not explicitly using web...

my understanding is that web middleware is applied by default to web.php

i might be mistaken.

app\Http\Company\CompanyController is where the session is set.

public function setCompany(Request $request) {

       $company_id = $request->input('company_id');


        $request->session()->put('company_id', $company_id);

        return back();

but i am trying to use the session setting in blade and in other controllers sessions. other controllers are in different scopes.

in addition i think i need to mention that i am using the database driver, but i also reverted back to the file driver and it didn't fix the problem.

what i am sure of is that in the CompanyController the session is being set correctly because i responsed with session('company_id') and the response was correct.

but calling the session from elsewhere is returning null in the model. sometimes it set it to a value but cannot overwrite.

as u can notice up in the code i am also applying forget to make sure the session attribute is flashed but still this is not helping.

Thanks in advance for the patience you are talking to read all this.


To add to what robrogers3 mentioned, you can try this command to confirm if the web middleware is being applied to your route:

php artisan route:list

Hi Talinon,

i have verified all group routes are being attributed Auth and Admin (copy of web) in kernel... the routes are being used in a prefixed group ('admin'). i am attaching here below the kernel definition

'admin' => [
            // \Illuminate\Session\Middleware\AuthenticateSession::class,

i am doubting that the issue is related to prefixed routes...


The fact it works in your CompanyController, but none of the other controllers, suggests to me that it is persisting multiple sessions.

Since you reverted back to your file driver for sessions, try deleting everything in your storage/framework/sessions directory.

Login normally, then hit your CompanyController to see if an additional file is created.


Hi Talinon,

I did as indicated.

Your doubt was right, no file is created in the sessions directory.

Please sign in or create an account to participate in this conversation.