📖 Scenario: You are building a simple Laravel web app that needs to remember a user's favorite color during their visit.
🎯 Goal: Create a Laravel controller method that stores a favorite color in the session, then retrieves and displays it.
📋 What You'll Learn
Create a session key to store the favorite color
Set the favorite color value in the session
Retrieve the favorite color from the session
Return a view or response showing the favorite color
💡 Why This Matters
🌍 Real World
Web apps often need to remember user choices or data temporarily during a visit, like login status or preferences.
💼 Career
Understanding sessions is essential for backend web development to manage user state and data securely.
Progress0 / 4 steps
1
Create a controller method with a favorite color variable
In a Laravel controller method called showFavoriteColor, create a variable named favoriteColor and set it to the string 'blue'.
Laravel
Hint
Use $favoriteColor = 'blue'; inside the method.
2
Store the favorite color in the session
Inside the showFavoriteColor method, add a line to store the $favoriteColor variable in the session using the key 'color'. Use Laravel's session() helper function.
Laravel
Hint
Use session(['color' => $favoriteColor]); to save the value.
3
Retrieve the favorite color from the session
Add a new variable called storedColor that retrieves the value from the session using the key 'color'. Use the session() helper function to get the value inside the showFavoriteColor method.
Laravel
Hint
Use $storedColor = session('color'); to get the value.
4
Return a response showing the stored favorite color
Complete the showFavoriteColor method by returning a simple string response that says 'Your favorite color is: ' followed by the $storedColor variable. Use Laravel's response() helper function.
Laravel
Hint
Use return response('Your favorite color is: ' . $storedColor); to send the text.
Practice
(1/5)
1. What is the main purpose of sessions in Laravel?
easy
A. To store user data across multiple requests
B. To compile CSS and JavaScript files
C. To send emails to users
D. To manage database migrations
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 A
Quick Check:
Sessions = store user data [OK]
Hint: Sessions keep data between pages, not for emails or files [OK]
Common Mistakes:
Confusing sessions with asset compilation
Thinking sessions send emails
Mixing sessions with database tasks
2. Which of the following is the correct way to store a value in Laravel session?
easy
A. session('key' => 'value');
B. Session::put('key', 'value');
C. session()->put('key', 'value');
D. session->put('key', 'value');
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 C
Quick Check:
Use session()->put() to store data [OK]
Hint: Use session() helper with ->put() to save data [OK]
Common Mistakes:
Using invalid syntax like session('key' => 'value')