HartleySan
HartleySan

Reputation: 7810

Why is my Laravel 5.7 session not working?

I'm trying to get sessions to work in Laravel 5.7, but I can't seem to get it working; I'm not sure why. I've looked at the official docs, looked at a bunch of SO posts, done Googling for the issue on other sites, and I still can't figure it out.

Basically, I've got a route like the following in web.php:

Route::get('/some_path', 'SomeController@index');

Then, in the index method of SomeController, I have the following:

session(['test', 'Some Value']);
session(['test2', 'Some Other Value']);
session(['test3', 'Some Third Value']);
$value = session('test', 'Backup Value');
echo $value;

With that, I'm always getting Backup Value echoed to the screen.

If I go into /storage/framework/sessions, I see a session file that has the following content (which I have anonymized):

a:5:{s:6:"_token";s:40:"token-here";s:9:"_previous";a:1:{s:3:"url";s:26:"http://example.com/some_path";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}i:0;s:5:"test3";i:1;s:16:"Some Third Value";}

Basically, it looks like the session is kind of working, but it only seems to store test3. If I comment out the test3 line in the controller, then it only stores test2. And even if I only have the test line in the controller, it still doesn't retrieve the value.

I also tried switching the session to a DB session with the proper session migrate file a la https://laravel.com/docs/5.7/session, but it's still not working.

Basically, I have no clue what's up. I can say that I'm not using the native Laravel auth, so I'm not sure if that matters, but I feel like it shouldn't. Also, I tried restarting my localhost, but that didn't make a difference and running both of the following commands, neither of which seem to change anything:

php artisan config:clear
php artisan route:clear

Lastly, I tried adding use Session at the top of the controller, but that doesn't seem to matter either.

Basically, I don't know what to do. What could possibly be causing this? Thank you.

Upvotes: 3

Views: 18227

Answers (6)

John Hunt
John Hunt

Reputation: 4072

If for some reason your code doesn't run through - eg you're using dd() somewhere, then your session will never save. This tripped me up for a good 20 minutes.

Upvotes: 1

AdamJones
AdamJones

Reputation: 601

Session::save() can be a life saver, definitely try that if still issues.

Upvotes: 4

Ashish Gaur
Ashish Gaur

Reputation: 77

those who are unable to save the session the issue is you guys are forgot to put the save() function after put given is the example.

$cartCount = Session::get('cartCount');
        if(empty($cartCount)){
            $cartCount = 0;
            Session::put('cartCount', $cartCount);
        }
        Session::save();

Upvotes: 4

Andino Inyang
Andino Inyang

Reputation: 109

In this case, all you need to do is add a "web" middleware chain function to your route rule. e.g

Route::get('/some_path', 'SomeController@index')->middleware('web');

Then clear cache:

php artisan config:clear
php artisan route:clear

Upvotes: 0

Kamlesh
Kamlesh

Reputation: 6135

Laravel 5.7 - store value in session with File system:

Set the value in session:

session()->put('email', '[email protected]');

Get the value from session:

$email = session()->get('email');

Delete the session of email:

session()->forget('email');

It works like charm :)

Upvotes: 3

Deepak Singh
Deepak Singh

Reputation: 650

What's happening here is that when you add this:

session(['test', 'x'])

It adds test at the 0th index of the session and x at 1st. Now when you again do a

session(['test1', 'xx'])
It overwrites the 0th and 1st index of session with new values. Therefore when you print the session data you get the last values. You can check this by doing a dd(session()->all()) and seeing the same on screen. If you want to make a key value relation and store the data in such away, please use the syntax like this:

session(['test' => 'x']);
session(['test1' => 'xx']);

Upvotes: 8

Related Questions