Laravel 9 Flash Message Example Tutorial

Hello Artisan

In this Laravel 9 session flash message tutorial, I will show you how we can show session flash message in Laravel 9 after submitting a form. You know that using flash messages are needed in laravel 9 application because that way we can show  easily what is happening with the request which is sent by the user.

We can easily show error, warning, success etc. And i think it is good practice to use flash message in web application. So i am here to share with you how to use flash message in Laravel 9 application.

I will use package to show session flash message. You can do it without using packages. We can esily customize this flash message before showing to user. Let's see how to show and use session flash message in Laravel 9 application.


Step 1 : Install Package

First of all install flash package via composer command. composer package offers a Twitter Bootstrap optimized flash messaging setup for your Laravel applications.

composer require laracasts/flash


Step 2 : Create Route

We need route show html form and submit it to database. So now paste this following code to create laravel flash message example.


Route::get('/test', function(){
  return view('test');

Route::post('/test', function(Request $request){
    'name' => $request->name
  flash('Data submitted')->important();
  return redirect()->back();


Step 3 : Create Blade File

Now create test.blade.php to create html form where we will show our flash message after submitting the form to the server.



Now if you hit the submit button you will see the flash message. If you need to modify the flash message partials, you can run:

php artisan vendor:publish --provider="Laracasts\Flash\FlashServiceProvider"


Hiding Flash Messages

A common desire is to display a flash message for a few seconds, and then hide it. To handle this, write a simple bit of JavaScript. For example, using jQuery, you might add the following snippet just before the closing body tag.


You may also do:

flash('Message')->success(): Set the flash theme to "success".
flash('Message')->error(): Set the flash theme to "danger".
flash('Message')->warning(): Set the flash theme to "warning".
flash('Message')->overlay(): Render the message as an overlay.
flash()->overlay('Modal Message', 'Modal Title'): Display a modal overlay with a title.
flash('Message')->important(): Add a close button to the flash message.
flash('Message')->error()->important(): Render a "danger" flash message that must be dismissed.


Read also: Laravel 9.x Toastr Notifications Example Tutorial


Hope this laravel 9 flash message tutorial will help you.


#laravel #laravel-9x