Laravel 5.4 Show flash error and success messages in different ways

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Displaying Error and Success Messages in Laravel 5.4 - A Comprehensive Guide Introduction: In this blog post, we will discuss various ways of displaying error and success messages in your Laravel applications using the latest version, Laravel 5.4. We'll provide a clear example to help you implement these techniques effectively. By following these best practices, you can ensure a better user experience while interacting with your web application. 1. Using Session Flash Messages: To show error and success messages in Laravel applications, we often use session flash messages. These messages are stored temporarily, ensuring they are only displayed once before being cleared from the session. Let's take a look at how to implement this method in our code. In your controller, you can add the following code to display error and success messages based on the outcome of an action: IndexController.php:
public function store(PostRequest $request) {
    if (Post::create($request->all())) {
        $request->session()->flash('status', 'Post was successfully added!');
    } else {
        $request->session()->flash('status', 'Error!');
    }
    return redirect('/');
}
The code above checks if the post is created successfully or encounters an error. In both cases, it sets a flash message using the session object. Here, we are displaying the messages with a key called 'status.' 2. Displaying Flash Messages in Views: To show the messages on your web application's page, you can use the conditional syntax provided by Laravel. This ensures that only active flash messages are displayed to the user. Below is an example of displaying a message when session has('status') is true: IndexView.blade.php:
<?php if(session()->has('status')){
        echo '<div style="text-align: center">';
        echo session()->get('status');
        echo '</div>';
    }?>
You can add your own custom CSS and HTML to make the messages more visually appealing. Ensure that you only display flash messages once, as they are designed for temporary use. 3. Best Practices: When working with error and success messages in Laravel applications, it is essential to adhere to the following best practices: - Ensure your messages are concise and clear, providing users with a detailed understanding of the issue or successful outcome. Avoid generic messages such as "An Error Occurred" or "Success!" - Make sure you only display a message once per session. This prevents it from being shown multiple times when navigating between pages in the application. - Add appropriate CSS and HTML styling to ensure that your messages are visually appealing and align with your brand guidelines. - Use conditional statements, as shown above, to only display active flash messages based on their status. Conclusion: In conclusion, displaying error and success messages in Laravel 5.4 is a crucial aspect of creating an engaging user experience for your web application. By following best practices and using session flash messages, you can effectively communicate the appropriate message to users while ensuring they are displayed only once. Always strive to keep your messages concise and clear, utilizing styling that complements your branding to enhance their impact.