Laravel's session provides simple key-value storage that persists across requests for a given user — the basic get/set API, along with a few less obvious methods, covers the large majority of real session use cases.
Setting a session value
$request->session()->put('cart_id', $cartId);
// Using the global session() helper instead
session(['cart_id' => $cartId]);
Getting a session value
$cartId = $request->session()->get('cart_id');
// With a default value if the key doesn't exist
$cartId = $request->session()->get('cart_id', null);
// Using the helper
$cartId = session('cart_id');
Checking if a key exists
if ($request->session()->has('cart_id')) {
// key exists AND is not null
}
if ($request->session()->exists('cart_id')) {
// key exists, even if its value is null
}
has() returns false for a key that exists but holds a null value — exists() checks purely for the key's presence regardless of its value, a subtle but genuinely important distinction when null is itself a meaningful stored value rather than an absence signal.
Removing a specific value
$request->session()->forget('cart_id');
// Removing multiple keys at once
$request->session()->forget(['cart_id', 'discount_code']);
Clearing the entire session
$request->session()->flush();
flush() removes every value from the session entirely — a meaningfully more destructive operation than forget(), which only removes the specific keys named, worth reserving for something like a full logout rather than routine data cleanup.
Getting a value and removing it in one call
$discountCode = $request->session()->pull('discount_code');
pull() retrieves a value and removes it from the session in a single atomic operation — genuinely useful for a one-time-use value like a discount code that should be applied exactly once and never accidentally reused from a stale session value.
Incrementing a numeric session value
$request->session()->increment('page_views');
$request->session()->increment('points', 10); // increment by a specific amount
Flash data, and its relationship to regular session data
$request->session()->flash('status', 'Profile updated!');
Following the flash-message pattern covered elsewhere on this site, flash data is stored through this exact same session mechanism — the only difference is that Laravel automatically schedules it for removal after the very next request, rather than persisting indefinitely the way a normal put() value does.
Reflashing data to survive one more request
$request->session()->reflash(); // keeps ALL flash data for one more request
$request->session()->keep(['status']); // keeps only specific flash keys
This is genuinely useful for a multi-step redirect chain (a redirect that itself triggers another redirect) where the flash data needs to survive past the single request it would otherwise be cleared after.
Storing an array or object in the session
$request->session()->put('cart_items', $items); // $items can be an array or Collection
Laravel's session storage is not limited to simple scalar values — an array, a Collection, or any other serializable structure works the same way, which is exactly what makes storing something like an entire shopping cart's contents directly in the session a viable approach for a small guest-cart feature.