0
0
Laravelframework~20 mins

Session basics in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Session Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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";
}
AUser: Alice
BUser:
Cnull
DError: Undefined variable
Attempts:
2 left
💡 Hint
Remember that Laravel's session put and get methods store and retrieve data within the same request cycle.
state_output
intermediate
2: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');
}
A1
B0
Cnull
DError: Method not found
Attempts:
2 left
💡 Hint
The session get method returns the default value if the key does not exist.
📝 Syntax
advanced
2: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?
A$theme = session()->get('theme', default='light');
B$theme = session()->get('theme', 'light');
C$theme = session()->get('theme') ?? 'light';
D$theme = session()->get('theme' ?: 'light');
Attempts:
2 left
💡 Hint
Check the Laravel session get method signature for default value usage.
🔧 Debug
advanced
2: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');
}
AError because session() helper is undefined
BError because forget() method requires a second argument
CNo error; returns null because 'user' key is removed
DError because session()->get() is called without Request object
Attempts:
2 left
💡 Hint
Check Laravel session helper usage and method signatures.
🧠 Conceptual
expert
2: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.
AIt resets the session ID but preserves all session data.
BIt deletes only the session cookie but keeps data on server.
CIt clears only flash data but keeps other session data intact.
DIt removes all session data for the current user immediately.
Attempts:
2 left
💡 Hint
Think about what flushing a session means in terms of stored data.