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

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Mastering Flash Messages in Laravel Sessions: Types, Best Practices, and Implementation Introduction: Flash messages are a critical component of any web application as they provide instant feedback to users about their actions. In Laravel, there are several ways to handle session flash messages depending on the severity level, complexity of the message or type. This blog post will walk you through various methods for passing different types of flash messages in Laravel's sessions and showcase the best practices for displaying these messages in your views. 1. Using Redirect::with() for Redirecting with Flash Messages:
Redirect::to('users/login')->with(['success' => 'Thanks for registering!', 'warning' => 'You missed this step!']);
In this example, the controller action redirects to another route while passing two flash messages: a success message and a warning message. The messages are stored as array keys inside the with() function. This is one of the most common approaches when combining redirection with flash messages. 2. Using Session::flash() for Simple Message Display:
Session::flash('message', 'This is a message!');
If you need to display a simple message without any specific severity level or additional metadata, this method works perfectly fine. Just set the message key and value directly in the Session::flash() function. 3. Passing Different Message Types with Separate Keys:
Session::flash(['success' => 'Thanks for registering!', 'warning' => 'You missed this step!']);
In this approach, you can set multiple message types in a single call using an array of keys and values. This method allows you to handle different severity levels or metadata within the session, which is useful when your application deals with complex scenarios and requires more flexibility for flash messages. 4. Using Message Classes from Bootstrap:
Session::flash('message_danger', 'This is a nasty message! Something's wrong.');
The previous example shows how you can use the session to assign different classes based on the severity level of your messages. In this case, we are setting the message type using a separate key: 'message_danger'. This approach enables you to fully leverage the available Bootstrap alerts while still keeping the messages stored in the session. A more robust implementation could involve creating an array of predefined message types with their respective classes. 5. Displaying Messages Based on Message Types in Blade:
// In your master layout or anywhere you wish to display messages
'@if(Session::has('message'))
    <p class="alert alert-info">{{ Session::get('message') }}</p>
'@elseif(Session::has('message_danger'))
    <p class="alert alert-danger">{{ Session::get('message_danger') }}</p>
'@elseif(Session::has('message_warning'))
    <p class="alert alert-warning">{{ Session::get('message_warning') }}</p>
'@endif
This example demonstrates how to display messages based on their type. You can check for common message types, such as 'message', 'success', 'warning', or 'danger' and assign the corresponding class using if-else statements in your blade template. The syntax is cleaner when you use shorter keys for the types like 'success', 'warning', or 'danger'. Conclusion: Flash messages are a critical component of any Laravel application, as they provide instant feedback to users and can significantly improve their experience. In this post, we have discussed various methods to pass different types of flash messages in the session, including using Redirect::with(), Session::flash(), setting separate keys for message types, and displaying them in your views based on their type. Employing these best practices will make your application more user-friendly and efficient, ensuring better communication between users and your Laravel app.