Using Flash Messages for User Feedback in Laravel
Learn how to implement session flash messages in your Laravel Task Manager to provide immediate, professional feedback after users save or delete data.
Previously in this course, we finished completing CRUD functionality. While our Task Manager now successfully saves, updates, and deletes tasks, the user experience is lacking: the page just refreshes, leaving the user wondering if their action actually worked.
In this lesson, we will bridge that gap by implementing flash messages. These are temporary bits of data stored in the session that persist only for the very next request, making them the perfect tool for success or error notifications.
Understanding Flash Messages
In web development, a "flash" is a specific type of session data that automatically clears itself after it is displayed once. Because HTTP is stateless, we need a way to carry a message from the controller (where the action happens) to the view (where the user sees the result).
Laravel makes this effortless. When you redirect a user, you can chain a with() method to the redirect response.
The Controller Logic
Let's update our TasksController to include a success message whenever a task is created or updated.
PHP#6A9955">// app/Http/Controllers/TasksController.php public function store(Request $request) { $validated = $request->validate([ 'title' => 'required|max:255', ]); $request->user()->tasks()->create($validated); #6A9955">// The 'with' method flashes data to the session return redirect()->route('tasks.index') ->with('success', 'Task created successfully!'); }
When you call ->with('success', '...'), Laravel stores that key-value pair in the session for the next request. Once the next page loads and you display that message, Laravel automatically deletes it.
Displaying Feedback in Blade
Now that the message is in the session, we need a way to render it consistently across our application. The best place for this is in your master layout file, which we built in Task Manager: Building the User Interface with Blade.
Open your resources/views/layouts/app.blade.php and add this code right above your @yield('content'):
HTML<!-- resources/views/layouts/app.blade.php --> @if (session('success')) style="color:#808080"><style="color:#4EC9B0">div class="alert alert-success" style="padding: 15px; background: #d4edda; color: #155724; border: 1px solid #c3e6cb; margin-bottom: 20px;"> {{ session('success') }} style="color:#808080"></style="color:#4EC9B0">div> @endif @yield('content')
Styling Success and Error Alerts
While the inline styles above work, in a production app, you’ll typically use CSS classes (like those from Tailwind or Bootstrap). You should also account for different types of messages, such as errors or warnings.
A robust pattern is to check for the presence of the session key and wrap the output in a styled container. You can expand this to handle multiple types:
HTML@if (session()->has('success')) style="color:#808080"><style="color:#4EC9B0">div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded"> {{ session('success') }} style="color:#808080"></style="color:#4EC9B0">div> @endif @if (session()->has('error')) style="color:#808080"><style="color:#4EC9B0">div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded"> {{ session('error') }} style="color:#808080"></style="color:#4EC9B0">div> @endif
Hands-on Exercise
- Update your Controller: Modify your
destroymethod inTasksController.phpto include a flash message:return redirect()->route('tasks.index')->with('success', 'Task deleted!'); - Add the UI: Ensure the
@ifblock forsuccessis present in your master layout. - Test: Create a new task and delete an existing one. You should see the green banner appear at the top of the index page immediately after the redirect.
Common Pitfalls
- Forgetting the Redirect: Flash messages only work during a redirect. If you return a
view()directly, the session data won't "flash" because the request cycle hasn't finished. Always useredirect()->route(...)for actions that change database state. - Hardcoding in Every View: Don't put your alert HTML inside every individual view file. By placing it in your master layout, you ensure that any controller can trigger a message and it will automatically appear in the UI without extra work.
- Session Lifetime: If you use
session()->put('key', 'value')instead ofwith(), the data will stay in the session until you manually delete it. Always usewith()for short-term UI feedback.
Recap
Flash messages are the industry-standard way to provide UX feedback. By using with() in your controllers and checking the session() helper in your Blade templates, you provide clear, temporary confirmation of user actions. This keeps your application feeling responsive and professional.
Up next: Task Manager: Adding Status and Priorities — we’ll expand our database schema to allow for more complex task tracking.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.