Laravel Tutorials

Working with Flash Session Data in Laravel

Published
Working with Flash Session Data in Laravel image

Laravel offers an intuitive system for managing temporary session data through its flash methods. This approach is ideal for implementing status messages, alerts, and other ephemeral notifications in your application.

The basic implementation stores data for just the next request:

$request->session()->flash('status', 'Task was successful!');

Laravel provides several specialized flash methods for different scenarios:

// Extend all flash data for another request
$request->session()->reflash();

// Selectively extend specific flash items
$request->session()->keep(['username', 'email']);

// Flash data available only in the current request
$request->session()->now('status', 'Operation completed');

Here's how these methods work in a practical notification system:

class NotificationController extends Controller
{
    public function processForm(Request $request)
    {
        try {
            DB::transaction(function () use ($request) {
                // Process form data
                $result = $this->processData($request->all());
                
                // Flash success for next request
                $request->session()->flash('message', 'Form processed successfully');
                $request->session()->flash('details', [
                    'id' => $result->id,
                    'timestamp' => now()->toDateTimeString()
                ]);
                
                // Conditional keep based on user preference
                if ($request->has('show_details')) {
                    $request->session()->keep(['details']);
                }
            });
            
            return redirect()->route('dashboard');
            
        } catch (Exception $e) {
            logger()->error('Form processing failed', ['error' => $e->getMessage()]);
            
            // Immediate error for current request
            $request->session()->now('error', 'Processing failed');
            
            return back()->withInput();
        }
    }
}

Flash session data creates a streamlined approach to request-based messaging without the complexity of persistent storage mechanisms, making your application more responsive and user-friendly.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article