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

luijay's avatar

How to use session in controller

Hi, I'm trying to use Session in controller. Can the stored value in session be used in laravel controller ? I can store data to session from my controller but whenever I try this die(var_dump(Session::get('variable'))); It says undefined variable session. This is how I store my value in session -> Session::put('variable', $value); and this is how I get my data Session::get('variable');. The problem is can session get be used in different controller ? by just calling session::get('variable');

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Yes of course.

  1. If you dd() then session data is not written to the session unless you session()->save() before the dd. This is because session data is persisted to the session storage by terminable middleware and if you dd or your application crashes then the session data might not be saved.

  2. install barryvhd/laravel-debugbar . It will show you the data stored in session after each http request.

1 like
luijay's avatar

Thanks got it. that's why my data isn't showing because I need to save it first.

Snapey's avatar

You should not need to save it. It will be saved automatically if you let the request cycle complete.

Please mark best answer if your problem is solved.

Please or to participate in this conversation.