Lesson 18 of 50 - Learn how to store and retrieve temporary user data.
A session allows an application to store information about a user across multiple requests. Unlike ordinary local variables, session data can remain available when the user opens another page.
Examples of session data include:
Laravel provides a session system that supports several storage drivers, including file, database, cookie, Redis, and others.
You can use the session() helper to store
data in the current session.
Route::get('/set-session', function () {
session(['student_name' => 'Rahul']);
return 'Session data stored successfully!';
});
In this example, Laravel stores the value
Rahul using the session key
student_name.
The session is persisted by Laravel according to the configured session driver.
Use the session() helper to retrieve
a value stored in the session.
Route::get('/get-session', function () {
$name = session('student_name', 'Guest');
return 'Student Name: ' . $name;
});
If the session key exists, Laravel returns its value.
Otherwise, it returns the default value
Guest.
Open /set-session first and then
/get-session to test the example.
You can store multiple values using different session keys.
Route::get('/student-session', function () {
session([
'student_name' => 'Amit',
'student_id' => 101,
'course' => 'Web Development'
]);
return 'Student session data stored!';
});
Route::get('/student-details', function () {
$name = session('student_name', 'Unknown');
$id = session('student_id', 'Not available');
$course = session('course', 'Not selected');
return "Name: $name | ID: $id | Course: $course";
});
This example stores a student's name, ID, and course in the session and retrieves them on another request.
Use session()->has() to check whether a
session key exists and contains a non-null value.
Route::get('/check-session', function () {
if (session()->has('student_name')) {
return 'Student session exists!';
}
return 'Student session not found!';
});
To check whether a key exists even when its value is
null, use session()->exists().
if (session()->exists('student_name')) {
// The key exists, even if its value is null.
}
Use session()->forget() to remove a specific
key from the session.
Route::get('/remove-session', function () {
session()->forget('student_name');
return 'Student name removed from session!';
});
To remove multiple keys:
session()->forget([
'student_name',
'student_id'
]);
To remove all session data, use
session()->flush(). Be careful because
this clears all data from the current session.
session()->flush();
Flash data is intended to be available for the next request, making it useful for temporary messages.
Route::get('/save-flash', function () {
return redirect('/show-flash')
->with('message', 'Welcome to Laravel!');
});
Route::get('/show-flash', function () {
return view('show_flash');
});
Create the Blade file:
resources/views/show_flash.blade.php
<h2>Flash Message Example</h2>
@if (session('message'))
<p style="color:green;">
{{ session('message') }}
</p>
@endif
Laravel makes the flash message available during the next request. It is normally removed after that request unless it is flashed again or kept for another request.
You can access session data inside a controller by using the request object.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function saveSession(Request $request)
{
$request->session()->put(
'student_name',
'Priya'
);
return 'Student name saved in session!';
}
public function showSession(Request $request)
{
$name = $request->session()->get(
'student_name',
'Guest'
);
return 'Student Name: ' . $name;
}
public function removeSession(Request $request)
{
$request->session()->forget('student_name');
return 'Student session removed!';
}
}
The request session methods provide another way to store, retrieve, and remove session values.
Laravel's session storage driver is configured using
the SESSION_DRIVER environment variable.
For file-based session storage, a typical setting is:
SESSION_DRIVER=file
Other available drivers can include:
file — stores session data in files.database — stores session data in a database table.cookie — stores session data in an encrypted cookie.redis — uses Redis for session storage.Driver availability depends on your Laravel version and application configuration. For database sessions, ensure the required sessions table exists.
Practical Assignment:
1. What is the main purpose of a session?
2. Which method removes a specific session key?
3. What is flash data commonly used for?