0
0
Laravelframework~10 mins

Session basics in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Session basics
User sends HTTP request
Laravel reads session cookie
Retrieve session data from storage
Controller accesses/modifies session
Laravel saves session data
Response sent with updated session cookie
User browser stores updated cookie
This flow shows how Laravel reads, modifies, and saves session data during a web request.
Execution Sample
Laravel
<?php
// Store data in session
session(['user_name' => 'Alice']);

// Retrieve data from session
$name = session('user_name');

// Remove data from session
session()->forget('user_name');
This code stores, retrieves, and removes a value in Laravel's session.
Execution Table
StepActionSession Data BeforeSession Data AfterOutput/Result
1Store 'user_name' => 'Alice'{}{"user_name": "Alice"}No output
2Retrieve 'user_name'{"user_name": "Alice"}{"user_name": "Alice"}"Alice"
3Remove 'user_name'{"user_name": "Alice"}{}No output
4Retrieve 'user_name' after removal{}{}null (no value)
💡 Session data is empty after removal; retrieval returns null.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
session data{}{"user_name": "Alice"}{"user_name": "Alice"}{}{}
$nameundefinedundefined"Alice""Alice"null
Key Moments - 2 Insights
Why does retrieving 'user_name' after removal return null?
Because in step 3 the 'user_name' key is removed from the session, so in step 4 it no longer exists, returning null as shown in the execution_table.
Does storing data in session immediately output anything?
No, storing data in session (step 1) changes session storage but does not produce output, as shown by 'No output' in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the session data after step 1?
A{"user_name": "Alice"}
B{}
Cnull
D{"user_name": null}
💡 Hint
Check the 'Session Data After' column for step 1 in the execution_table.
At which step does the session data become empty again?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Session Data After' column to see when the session data changes to {}.
If you try to retrieve 'user_name' before storing it, what would you get?
A"Alice"
BError
Cnull
DEmpty string
💡 Hint
Refer to the variable_tracker for $name before step 1 and after step 4.
Concept Snapshot
Laravel sessions store data across requests.
Use session(['key' => value]) to save data.
Retrieve with session('key').
Remove with session()->forget('key').
Session data persists until removed or expired.
Full Transcript
In Laravel, sessions let you save data between web requests. When a user sends a request, Laravel reads the session cookie and loads stored data. You can store data using session(['key' => value]), retrieve it with session('key'), and remove it with session()->forget('key'). After changes, Laravel saves the session data and sends an updated cookie back to the browser. This way, data like user names or preferences stay available as the user moves through the site. The execution table shows storing 'user_name' as 'Alice', retrieving it, removing it, and then trying to retrieve again returns null because the data was removed.