Sessions help remember information about a user while they browse your website. This way, the site can keep track of who they are and what they do.
Session basics in Laravel
<?php // Store data in session session(['key' => 'value']); // Get data from session $value = session('key', 'default'); // Remove data from session session()->forget('key'); // Clear all session data session()->flush();
Use the session() helper to easily work with session data.
You can store, retrieve, and remove data using simple methods.
<?php // Store user's name in session session(['username' => 'Alice']);
<?php // Retrieve username or default to 'Guest' $name = session('username', 'Guest');
<?php // Remove username from session session()->forget('username');
<?php // Clear all session data session()->flush();
This example shows three routes: one to set a session value, one to get it, and one to clear all session data. When you visit /set-session, it saves 'blue' as the color. Visiting /get-session shows the saved color. Visiting /clear-session removes all session data.
<?php use Illuminate\Support\Facades\Route; Route::get('/set-session', function () { session(['color' => 'blue']); return 'Session color set to blue'; }); Route::get('/get-session', function () { $color = session('color', 'no color set'); return "The session color is: $color"; }); Route::get('/clear-session', function () { session()->flush(); return 'Session cleared'; });
Sessions use cookies to link the user to their stored data.
Make sure your app has session middleware enabled to use sessions.
Session data is stored on the server, so it is safer than storing data in cookies.
Sessions keep user data while they browse your site.
Use session() helper to store, get, and remove data.
Clear sessions when you want to forget all stored info.
Practice
Solution
Step 1: Understand what sessions do
Sessions keep user data while they browse your site, allowing data to persist between pages.Step 2: Match purpose with options
Only To store user data across multiple requests describes storing user data across requests, which is the session's role.Final Answer:
To store user data across multiple requests -> Option AQuick Check:
Sessions = store user data [OK]
- Confusing sessions with asset compilation
- Thinking sessions send emails
- Mixing sessions with database tasks
Solution
Step 1: Recall Laravel session syntax
To store data, use the session() helper with the put method: session()->put('key', 'value');Step 2: Check each option
A has syntax error (unexpected =>); B uses facade but not imported; C uses correct helper and method; D uses wrong arrow syntax.Final Answer:
session()->put('key', 'value'); -> Option CQuick Check:
Use session()->put() to store data [OK]
- Using invalid syntax like session('key' => 'value')
- Forgetting parentheses after session()
- Using wrong arrow (->) on session helper
session()->put('name', 'Alice');
$name = session('name');
echo $name;Solution
Step 1: Store 'Alice' in session under 'name'
The code uses session()->put('name', 'Alice') to save the value 'Alice'.Step 2: Retrieve and echo the stored value
session('name') fetches the value 'Alice' from session and echoes it.Final Answer:
Alice -> Option BQuick Check:
Stored value 'Alice' is retrieved and printed [OK]
- Expecting null if session not started
- Confusing session() helper with facade
- Assuming session() returns array
session()->put('user', 'Bob');
session()->forget('users');Solution
Step 1: Check stored and forgotten keys
The code stores 'Bob' under 'user' but tries to forget 'users' (plural), which was never stored.Step 2: Understand forget() behavior
forget() silently does nothing if the key doesn't exist; no error occurs but the intended key is not removed.Final Answer:
Key 'users' does not exist, so forget() does nothing -> Option DQuick Check:
forget() on missing key does nothing [OK]
- Assuming forget() throws error if key missing
- Confusing forget() with remove() method
- Thinking session() helper can't call forget()
Solution
Step 1: Identify method to clear all session data
Laravel's session()->flush() removes all session data for the current user.Step 2: Verify other options
Options B, C, and D are not valid Laravel session methods.Final Answer:
session()->flush(); -> Option AQuick Check:
Use flush() to clear all session data [OK]
- Using non-existent methods like clear() or forgetAll()
- Trying to remove session keys one by one instead of flush()
- Confusing forget() with flush()
Ready to prove it?
Pass the test and this pattern moves unknown
