Sessions in Laravel
| Store values | |
| Flash data | |
| Forget keys | |
| Session drivers note |
Store values
The session keeps small values for one visitor across requests. Put a value under a key, then read it back with a default for first-time visitors.
Store ids and preferences here, never passwords or other secrets.
session()->put("theme", "dark"); $theme = session("theme", "light");
Flash data
Flash stores a value for the next request only, then deletes it. It fits status notes after a redirect, like settings saved or profile updated.
Read it in the view once and it is gone on the request after that.
session()->flash("status", "Profile updated.");
Forget keys
Remove single keys when they are no longer needed, for example at logout. Forget drops one key, while flush drops the whole session.
Prefer forget so unrelated values survive the cleanup.
session()->forget("theme"); session()->flush();
Session drivers note
The driver decides where session data lives between requests. File works for one server, cookie keeps data in the browser, while database and Redis share sessions across servers.
Set the driver in the env file and leave the code as is.
SESSION_DRIVER=file
Next: CSRF
Article author: Arthur Isaev