How to Create Custom Flash Message in Laravel 9 with Example

In this tutorial, you will learn how to create custom flash messages in Laravel with Bootstrap CSS framework and tactfully implement flash messages in the Laravel application.

Flash messages are oppressive for the eyes; thats because it profoundly strikes users’ attention regarding something vital since we are using Bootstrap UI to create Flash message UI, so it has multiple types of flash messages such as alert-success, alert info, alert danger, alert warning.

Flash messages grab users attention about something essential; this step by step laravel flash message example tutorial lets you create a flash message from starting till the end.

Laravel 9 Flash Message Example

Here is the list towards building alert notification and flash messages in Laravel:

  • Error alert message
  • Info alert message
  • Success alert message
  • Warning alert message
  • Validation alert message

Create Laravel Project

In this first step you have to create a laravel application:

composer create-project laravel/laravel laravel-flash-message-example --prefer-dist

Create Success Flash Message in Laravel

You have to add the following code in your laravel blade view file to create a relentless yet straightforward Laravel success flash message:

@if ($alertFm = Session::get('success'))
<div class="alert alert-success alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong>{{ $alertFm }}</strong>
</div>
@endif

Create Error Flash Message in Laravel

You have to define the following code in your blade view file to display the error flash message:

@if ($alertFm = Session::get('error'))
<div class="alert alert-danger alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong>{{ $alertFm }}</strong>
</div>
@endif

Create Warning Flash Message in Laravel

In this step you have to add the below code in your view file to display the warning flash message:

@if ($alertFm = Session::get('warning'))
<div class="alert alert-warning alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong>{{ $alertFm }}</strong>
</div>
@endif

Create Warning Flash Message in Laravel

In this step you have to add the below code in your view file to display the warning flash message:

@if ($alertFm = Session::get('info'))
<div class="alert alert-info alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong>{{ $alertFm }}</strong>
</div>
@endif

Display Success and Error Flash Messages with Sweet Alert in Laravel

The sweet alert is a javascript based plugin which makes displaying alert flash messages easy in Laravel.

@if ($alertFm = Session::get('success'))
  <script type="text/javascript">
     swal({
         title:'Its a big success.',
         text:"{{Session::get('success')}}",
         timer:4000,
         type:'success'
     }).then((value) => {
     }).catch(swal.noop);
 </script>
 @endif

Similarly, you can use the below code to display error flash message with sweet alert:

@if ($alertFm = Session::get('error'))
 <script type="text/javascript">
    swal({
        title:'Error occured',
        text:"{{Session::get('error')}}",
        type:'error',
        timer:4000
    }).then((value) => {
    }).catch(swal.noop);
</script>
@endif

Flash Messages with Redirect

You can bind the flash message with the redirect() function in your controller; for instance, that is how you can adjoin the error flash message with the redirect.

public function store(Request $request)
{
    return redirect()->route('index')
        ->with('error','This is error message');
}

Summary

Laravel flash message tutorial with example is over, i reckon you have understood the concept of how to integrate flash messages in laravel applications effortlessly.