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

Adrianc's avatar

Why session is not working in Laravel 5.1

I was testing Laravel 5.1's session feature in WAMP, following those steps:

First, composer create-proejct laravel/laravel laravel5.1 --prefer-dist Second, in routes.php, I added Route::get('/testsession', 'TestSessionController@index'); Third, in TestSessionController.php, I added the following:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;

class TestSessionController extends Controller
{
    //
    public function index()
    {
        Session::set('aa', 'bb');
        var_dump(Session::get('aa'));
    }
}

I first run the application, and it outputted string 'bb' (length=2). Then I commented Session::set('aa', 'bb'); and run again, and it outputted null.

Is there something wrong? (Sorry about the formating)

0 likes
3 replies
IvanBernatovic's avatar

I don't know why, but you can't get data from Session if you are var_dumping (or dd()) it, but if you directly return then it works. Bottom line, for intended use it works.

P.S. In documentation it is used 'put ' method instead of 'set,' so keep that in mind.

2 likes
Adrianc's avatar
Adrianc
OP
Best Answer
Level 1

Thanks for the help!

I finally came across this post and got it worked. The reason is that I was actually using 5.2, and I didn't release I was using this new release. Sorry for being misleading.

In Laravel 5.2, everything need to be given a web middleware in order to use cookies or sessions. It is not mentioned in Laravel upgrade guide though.

1 like
o2relax's avatar

If you do dump or dd before return when you first start the application, the answer does not come cookies. You need to have at least once been run the application return to the session attached to the user , and then do the dumps

Please or to participate in this conversation.