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

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article
Flock: A Free, Native Herd Alternative for Laravel image

Flock: A Free, Native Herd Alternative for Laravel

Read article
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article