Challenge - 5 Problems
Session Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel session code?
Consider this Laravel controller method that sets and retrieves session data. What will be the output when this method is called?
Laravel
<?php
public function showSession(Request $request) {
$request->session()->put('user', 'Alice');
$name = $request->session()->get('user');
return "User: $name";
}Attempts:
2 left
💡 Hint
Remember that Laravel's session put and get methods store and retrieve data within the same request cycle.
✗ Incorrect
The session key 'user' is set to 'Alice' and then immediately retrieved, so the output is 'User: Alice'.
❓ state_output
intermediate2:00remaining
What will be the session value after this code runs?
Given this Laravel code snippet, what is the value stored in session key 'count' after execution?
Laravel
<?php
public function incrementCount(Request $request) {
$count = $request->session()->get('count', 0);
$request->session()->put('count', $count + 1);
return $request->session()->get('count');
}Attempts:
2 left
💡 Hint
The session get method returns the default value if the key does not exist.
✗ Incorrect
Initially, 'count' is not set, so get returns 0. Then it is incremented by 1 and stored back, so the value is 1.
📝 Syntax
advanced2:00remaining
Which option correctly retrieves a session value with a default fallback?
In Laravel, you want to get the session value for key 'theme'. If it does not exist, return 'light' as default. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the Laravel session get method signature for default value usage.
✗ Incorrect
Option B uses the correct syntax: get(key, default). Option B is invalid syntax. Option B uses PHP null coalescing but does not provide default to get method. Option B uses invalid named argument syntax.
🔧 Debug
advanced2:00remaining
Why does this Laravel session code cause an error?
This code throws an error. What is the cause?
Laravel
<?php
public function clearSession() {
session()->forget('user');
return session()->get('user');
}Attempts:
2 left
💡 Hint
Check Laravel session helper usage and method signatures.
✗ Incorrect
The forget method removes the key. Then get returns null since the key no longer exists. No error occurs.
🧠 Conceptual
expert2:00remaining
What happens to session data when you call session()->flush() in Laravel?
Choose the correct description of the effect of calling session()->flush() in a Laravel application.
Attempts:
2 left
💡 Hint
Think about what flushing a session means in terms of stored data.
✗ Incorrect
session()->flush() removes all data stored in the session for the current user immediately.