Skip to main content

What is a session

HTTP is a stateless protocol. That is, HTTP itself has no mechanism for retaining user information across requests. Laravel’s session feature provides a way to persist user data across multiple requests. Save data you want to persist across requests—login state, items in a cart, form input in progress—in the session.

Session driver configuration

Session configuration lives in config/session.php. You can switch drivers via the SESSION_DRIVER environment variable (in your .env file).
Laravel supports these drivers out of the box:
In Laravel’s default project setup, the database driver is configured. Using the database driver requires a sessions table, but it’s included in the default migrations, so it works out of the box.

Session serialization

Laravel 13’s application skeleton sets the serialization option to json in config/session.php. The framework’s internal default remains php.
Changing this option from php to json invalidates all active user sessions. It can also cause errors if your application stores PHP objects in the session.
To preserve active sessions during an upgrade, explicitly keep this option set to php. If your application does not store PHP objects in the session and you are comfortable requiring users to authenticate again, you can use json for improved security.

Accessing the session

There are two ways to access the session: using methods on the Request instance, or using the global session() helper.

Using the Request instance

Type-hint Request on your controller method, then access the session via $request->session().

Using the global session() helper

The session() helper function can be called from anywhere—controllers, views, and so on.
Both approaches can be verified via assertSessionHas in tests. Inside a controller, using $request->session() makes the dependency explicit and clearer.

Manipulating the session

Retrieving data

Checking whether data exists

Note the difference between has and exists.

Storing data

Removing data

Flash data

Flash data is session data that is only available for the next request. Use it for information you want to display just once, like form submission success messages or error messages.

Storing data with flash()

This data is available on the next request but is automatically deleted afterward.

Extending flash data

Displaying flash messages in Blade

Placing flash message display in a layout file (like layouts/app.blade.php) shows them consistently across every page.

Practical example: showing a message after form submission

After submitting a form, saving a success message to the session, redirecting, and displaying it on the next page is a classic web-application pattern.
1

Define routes

2

Implement the controller

In the store method that receives form submissions, redirect with a success message after saving.
redirect()->with('success', '...') is a shortcut for passing flash data to the redirect target.
3

Display the message in the layout

Add flash message display to resources/views/layouts/app.blade.php.
4

Create the index view

After the post is saved and the user is redirected, the “Post created.” message will appear once at the top of this index page.

Pattern combined with validation errors

On validation failure, combine with back()->withErrors(). Laravel automatically flashes validation errors, so you can use the $errors variable in Blade.
flush() deletes all data in the session. Login state and everything else is lost as well, so when you only want to remove specific user data, use forget() with the specific keys.

Next steps

Validation

Learn how to validate form input data.
Last modified on August 21, 2026