Working with Laravel Session
Sessions provide a way to store information across multiple requests. In Laravel, the session configuration file is stored in config/session.php. First of all, review the options available to you in this file. You will get important documented information about where and how Laravel stores or manages session data. If you don’t specify your session driver, the default driver will be 'file', and sessions will be stored in storage/framework/sessions.
1. Retrieving Value from Session
There are two primary ways of working with session data in Laravel: the global session() helper function and via a type-hinted Request instance on a controller method.
Via The Request Instance
For the Request instance, you need to use Illuminate\Http\Request;
use Illuminate\Http\Request;
public function show(Request $request, $id)
{
// Retrieve a piece of data from the session...
$value = $request->session()->get('key');
// Second argument is the default value if the key does not exist
$value = $request->session()->get('key', 'default');
// You can also pass a Closure as a default value
$value = $request->session()->get('key', function () {
return 'default_value';
});
}
Via The Global Session Helper
// Retrieve a piece of data from the session...
$value = session('key');
// Retrieve data with a default value...
$value = session('key', 'default');
// Storing data via the helper...
session(['key' => 'value']);
2. Retrieving All Session Data
If you would like to retrieve all the available data in the session, you can use the all() method:
$data = $request->session()->all();
3. Checking If an Item Exists
To determine if a value is present in the session, you may use the has() method. The has() method returns true if the value is present and is not null.
if ($request->session()->has('users')) {
// The 'users' key exists and is not null...
}
4. Storing Data in the Session
To store data, you typically use the put() method of the Request instance or the session() helper:
// Via Request instance
$request->session()->put('key', 'value');
// Via Global Helper
session(['key' => 'value']);
5. Deleting Data from Session
The forget() method will remove the specified item from the session:
$request->session()->forget('key');
If you want to retrieve the value and then delete it, use the pull() method:
$value = $request->session()->pull('key', 'default');
To remove all items from the session, use flush():
$request->session()->flush();
6. Flash Data
Flash data is only available during the subsequent HTTP request and is then deleted. This is useful for status messages.
$request->session()->flash('status', 'Task was successful!');
7. Important: Session Persistence
Laravel writes session data at the very end of the request lifecycle. If you terminate the script early (e.g., using dd() or die()), the session will not save. To force an immediate save:
$request->session()->put('key', 'value');
$request->session()->save();
Bonus: 3 Advanced Session Tips
Tip 1: Pushing to Array Sessions
If your session key holds an array, you can use the push() method to add a new value to it without retrieving the whole array first.
$request->session()->push('user.teams', 'developers');
Tip 2: Re-flashing Data
If you need to keep your flash data for an additional request (for example, after a second redirect), you can use the reflash() or keep() methods.
// Keep all flash data for one more request
$request->session()->reflash();
// Keep only specific flash data
$request->session()->keep(['status', 'email']);
Tip 3: Regenerating the Session ID
To prevent session fixation attacks, you should regenerate the session ID periodically, especially after a user logs in. Laravel does this automatically during authentication, but you can do it manually:
$request->session()->regenerate();