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.
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.
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.
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')
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
destroy method in TasksController.php to include a flash message: return redirect()->route('tasks.index')->with('success', 'Task deleted!');@if block for success is present in your master layout.view() directly, the session data won't "flash" because the request cycle hasn't finished. Always use redirect()->route(...) for actions that change database state.session()->put('key', 'value') instead of with(), the data will stay in the session until you manually delete it. Always use with() for short-term UI feedback.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.
Learn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.
Read moreLearn how to use Blade layouts and sections to create a DRY, consistent UI for your Laravel application. Stop repeating code and master template inheritance.
Using Flash Messages for User Feedback
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation