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

afoysal's avatar

Set & Get session variable value

I am setting session variable in function of a controller like below.

use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
	public function store(Request $request)
	{
		session(['user_name' => $user_name]);
	}
}

I am trying to access that session variable in another function of another controller.

use Illuminate\Support\Facades\Session;

class DashboardController extends Controller
{
    public function __construct()
    {
        dd(session('user_name'));   // I am not getting value here
    } 
}

I am not getting value from Session Variable.

0 likes
5 replies
tykus's avatar

What is your session driver configured as?

1 like
Snapey's avatar

is it not just the authenticated user?

Snapey's avatar

Is this part of a web request (where you have sessions) and not API routes (where you do not)

1 like

Please or to participate in this conversation.