Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 38 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

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.

LaravelUXSessionFlash MessagesBladeWeb Developmentphpbackend

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

  1. Update your Controller: Modify your destroy method in TasksController.php to include a flash message: return redirect()->route('tasks.index')->with('success', 'Task deleted!');
  2. Add the UI: Ensure the @if block for success is present in your master layout.
  3. 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 use redirect()->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 of with(), the data will stay in the session until you manually delete it. Always use with() 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.

Previous lessonHandling File UploadsNext lesson Task Manager: Adding Status and Priorities
Back to Blog

Similar Posts

LaravelJune 25, 20263 min read

Customizing Validation Error Messages for Better Laravel UX

Learn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.

Read more
LaravelJune 25, 20263 min read

Using Blade Layouts and Sections: A Beginner's Guide

Learn how to use Blade layouts and sections to create a DRY, consistent UI for your Laravel application. Stop repeating code and master template inheritance.

Part of the course

Laravel Fundamentals: From Zero to Your First App

beginner · Lesson 38 of 52

  1. 1

    Setting Up the Local Development Environment

    4 min
  2. 2

    Installing Laravel and Exploring Directory Structure

    3 min
  3. 3

    Understanding the .env File and Configuration

    3 min
Read more
LaravelJune 25, 20263 min read

Task Manager: Adding Status and Priorities in Laravel

Enhance your Task Manager with status and priority fields. Learn how to evolve your database schema, update your Blade UI, and implement state toggling.

Read more
  • 4

    The Laravel Application Lifecycle

    4 min
  • 5

    Initializing the Task Manager Project

    3 min
  • 6

    Defining Basic Web Routes

    4 min
  • 7

    Using Route Parameters

    3 min
  • 8

    Creating Your First Controller

    3 min
  • 9

    Returning Responses and Redirects

    3 min
  • 10

    Task Manager: Implementing the Task List Route

    3 min
  • 11

    Introduction to Blade Templating

    3 min
  • 12

    Using Blade Layouts and Sections

    3 min
  • 13

    Implementing Blade Partials

    4 min
  • 14

    Mastering Blade Directives for Loops and Conditionals

    3 min
  • 15

    Task Manager: Building the User Interface

    3 min
  • 16

    Understanding Database Migrations

    3 min
  • 17

    Working with Eloquent Models

    3 min
  • 18

    Performing Basic CRUD Operations

    3 min
  • 19

    Seeding the Database

    3 min
  • 20

    Task Manager: Displaying Real Database Records

    3 min
  • 21

    Capturing User Input from Forms

    4 min
  • 22

    Introduction to Laravel Validation

    3 min
  • 23

    Customizing Validation Error Messages

    3 min
  • 24

    Using Form Requests for Validation

    3 min
  • 25

    Introduction to Authentication

    4 min
  • 26

    Protecting Routes with Middleware

    3 min
  • 27

    Understanding CSRF Protection

    3 min
  • 28

    Preventing Mass Assignment

    3 min
  • 29

    Task Manager: Securing the Application

    3 min
  • 30

    Introduction to Route Model Binding

    3 min
  • 31

    Updating Existing Records

    3 min
  • 32

    Deleting Records

    3 min
  • 33

    Using Named Routes

    3 min
  • 34

    Task Manager: Completing CRUD Functionality

    3 min
  • 35

    Introduction to Database Relationships

    3 min
  • 36

    Querying Related Data

    4 min
  • 37

    Handling File Uploads

    3 min
  • 38

    Using Flash Messages for User Feedback

    3 min
  • 39

    Task Manager: Adding Status and Priorities

    3 min
  • 40

    Introduction to Artisan Commands

    3 min
  • 41

    Debugging with Laravel Tinker

    3 min
  • 42

    Understanding Service Providers

    4 min
  • 43

    Using View Composers

    Coming soon
  • 44

    Task Manager: Refactoring for Clean Code

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Testing Forms and Validation

    Coming soon
  • 47

    Using Database Transactions

    Coming soon
  • 48

    Handling Global Exceptions

    Coming soon
  • 49

    Preparing for Production

    Coming soon
  • 50

    Environment Security Best Practices

    Coming soon
  • 51

    Managing Assets in Production

    Coming soon
  • 52

    Task Manager: Deployment Preparation

    Coming soon
  • View full course