0
0
Laravelframework~30 mins

Session basics in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Session basics
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use return response('Your favorite color is: ' . $storedColor); to send the text.