Flash Messages & Session Data
Flash messages are session data that exists only for the next request — perfect for success/error notifications after form submissions. Laravel's with() method on redirects sets flash data, and Blade reads it with session().
Flash Messages
// In controller — set flash message
return redirect()->route('posts.index')
->with('success', 'Post created successfully!');
return back()->with('error', 'Something went wrong.');
// In Blade — display flash messages
@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
@if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
// Reusable component: resources/views/components/flash.blade.php
@foreach (['success', 'error', 'warning', 'info'] as $type)
@if (session($type))
<div class="alert alert-{{ $type }}">{{ session($type) }}</div>
@endif
@endforeach
// Use in layout: <x-flash />Tip
Tip
Practice Flash Messages Session Data in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Route::resource generates all 7 CRUD routes with one line
Practice Task
Note
Practice Task — (1) Write a working example of Flash Messages Session Data from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Flash Messages Session Data is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.