In Laravel, the best way to pass different types of flash messages in the session

In Laravel, the best way to pass different types of flash messages in the session

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

In Laravel, the best way to pass different types of flash messages in the session

Flash messages are a staple of user experience management in Laravel. They allow you to inform users about actions that occurred during a request—like a successful registration or a failed login—that wouldn't otherwise be visible because of an immediate redirect. The core mechanism is straightforward: store data in the session and clear it after one view cycle.

The original question highlights a common dilemma: how do you associate specific CSS classes, like Bootstrap's alert-danger or alert-success, with these messages? There are two primary approaches to solving this, each with distinct trade-offs regarding code clarity and maintenance.

The Single Key Strategy

The most idiomatic approach in Laravel is to use a single session key (e.g., message) and store the type of message as part of the data itself. This keeps your view logic clean and avoids cluttering the session array with multiple keys that might expire or be accessed independently.

In your controller, you can pass an associative array containing both the text and the intended style. Laravel's session driver handles complex data structures like arrays just fine, so this is not a limitation of the underlying storage.

// Controller Action
return Redirect::to('users/login')
    ->with('message', [
        'type' => 'danger',
        'text' => 'Something went wrong. Please try again.'
    ]);

In your Blade template, you check for the key and then iterate or conditionally render the content based on the type attribute:

@if(session('message'))
    @php $msg = session('message'); @endphp

    <div class="alert alert-{{ $msg['type'] }}">
        {{ $msg['text'] }}
    </div>
@endif

This method is robust because it separates the content from the presentation logic. If you later decide to change your CSS framework or add a new message type, you only update the controller or the view's rendering logic, not the session structure itself.

The Separate Key Strategy

Your source material suggests an alternative: creating distinct keys like message_danger or message_success. While functionally valid, this approach introduces a specific pitfall regarding conditional logic in your views.

If you use separate keys, your Blade template must explicitly check for each one:

@if(session('message_info'))
    <div class="alert alert-info">...</div>
@endif

@if(session('message_warning'))
    <div class="alert alert-warning">...</div>
@endif

This quickly becomes unwieldy if you have many message types. It also fragments the session data, making debugging slightly harder since you can't easily inspect all flash messages in one go using dd(session()). Furthermore, it couples your view logic directly to specific key names, which can be brittle if a controller forgets to set a specific key or if the naming convention changes.

Why Data Structures Win

From an architectural standpoint, storing metadata (like the type) alongside content is generally preferred in Laravel applications. It aligns with the framework's tendency towards expressive, flexible data passing. By using an array structure, you future-proof your application. You aren't forced to create a new session key for every single UI variation; instead, the "type" becomes a property of the message object.

There is one important consideration regarding Bootstrap 3 versus newer versions. In modern Bootstrap (4 and 5), alert types are often applied via utility classes or data attributes rather than just CSS class names on the parent element, but the logic of passing danger, info, etc., remains conceptually identical. Ensure your view renders the class correctly for your specific version to avoid styling issues.

Conclusion

For most Laravel applications, passing a structured array with a type and text key is the cleanest solution. It reduces repetitive @if statements in your views and keeps your session data normalized. While separate keys work, they add unnecessary complexity to the view layer without providing significant benefits over a simple, typed object structure.

Our network