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

Lowelheim's avatar

Laravel 8 and Global Session

Hi,

I'm trying within my webapp to use the Laravel's global session to store some infos. I decided to use the database driver to store the informations.

I've followed the setup as described in the official documentations and in effect after the login I see in the DB's session table the record for the current session.

Then, inside the controller's construct function I store the variable in the global session as this:

 session()->put('variablename', $variable); 

To test it, I follow this code with a simple

var_dump(session()->get('variablename')) 

and it works fine.

The problem appears when in another controller I try to access this variable with another

session()->get('variablename');

and get a not so nice NULL.

What am I doing wrong? The official documentation is ok, but dig not so deep in database driver's topic, so probably I'm missing something.

An well anticipated thanks for the help.

Kind regards

Lowelheim

0 likes
12 replies
sr57's avatar

@lowelheim

The problem appears when in another controller

With same browser session?

Lowelheim's avatar

@sr57 yes sir, with the same browser session.

The app is quite simple. This is a representation of my web.php:

Route::group(['middleware' => ['auth', 'web']], function () {
    Route::prefix('firstgroup')->group(function () {
        Route::prefix('secondgroup')->group(function () {
			// the {id} is passed to FirstController where in the construct is stored
			// in a session variable (works perfectly)
            Route::get('/{id}', [App\Http\Controllers\FirstController::class, 'index']);
            Route::prefix('thirdgroup')->group(function () {
				// when I try in the construct of SecondController to access the variable
				// stored before (I'd say one click away), the variable returned is NULL
                Route::get('/', [App\Http\Controllers\SecondController::class, 'index']);
            });
        });
    });
});
sr57's avatar

@lowelheim

Can you a write/ share a simple test code that does not work for you?

Lowelheim's avatar

@sr57 the controllers are like these:

First Controller

namespace App\Http\Controllers;


use Illuminate\Http\Request;

class FirstController extends Controller
{
    protected $oneId;


    public function __construct(Request $request)
    {
        parent::__construct();
        $this->oneId = $request->route('id');
		session()->put('oneid', $this->oneId);
		// correct
		var_dump(session()->get('oneid'));
    }

    public function index($id)
    {
		// ... do stuff with  the $id

        return view(...);
    }
}

Second Controller

namespace App\Http\Controllers;

class GroupsController extends Controller
{
    protected $oneId;

    public function __construct()
    {
        parent::__construct();
        $this->oneId = session()->get('oneid'); 
		
		// NULL
		var_dump(session()->get('oneid'));
    }

    public function index()
    {
		// ... do stuff
        return view(...);
    }
}
sr57's avatar

So, you set your session variable in FirstController / index

dump this variable in SecondController / index

and get Null by just changing the url in your browser?

Snapey's avatar

I'm concerned your use of the term "Global session"

Note that this is a user's session, it is not global and is private to all requests by the same user

Make sure that your scripts complete properly as the session data is only stored at the end of the request, so if you dd() before the request cycle completes, you will not see session data.

If you understand all of the above, and it does not apply, then are sessions working in terms of authentication and remaining logged in?

Lowelheim's avatar

@Snapey thank in advance for both your and sr57 answers.

I use the session in the same way I used to with the $_SESSION and maybe is not the correct way.

However what you have written saying that "... this is a user's session, it is not global and is private to all requests by the same user..." maybe got some implications with the variable that is NULL in the SecondController?

All is happening within the same session, always logged in.

Snapey's avatar

@Lowelheim as long as you know that session is for a user so CAN be shared by two controllers accessed by the same browser session

sr57's avatar

@Lowelheim

Please redo your test by removing all code in your controllers except for session set & get

sr57's avatar

@lowelheim

In your second controller, put a get a the index function (as you answered yes before)

sr57's avatar

I use the session in the same way I used to with the $_SESSION and maybe is not the correct way.

Should be ... Laravel is Php

Please or to participate in this conversation.