Laravel 9 Flash Message Example

Laravel 9 Flash Message Example

Laravel 9 shows a flash message example; Through this tutorial, we will learn how to show flash messages with a redirect in laravel 9 apps.

This laravel 9 flash message example tutorial will add several ways to give a flash message like redirect with success message, redirect with an error message, redirect with a warning message, and redirect with info message. And as well as, use a bootstrap flash alert layout that way it displays a pretty layout.

Laravel 9 Flash Message Example

Use the following steps to implement flash messages in laravel 9 apps:

  • Success Flash Message
  • Error Flash Message
  • Warning Flash Message
  • Info Flash Message
  • Sweet Alert Flash Success Message
  • Sweet Alert Flash Error Message

1 – Success Flash Message

First of all, we need to add the below code in your HTML files:

@if(Session::has('success'))
    <div class="alert alert-success">
        {{Session::get('success')}}
    </div>
@endif

The second thing, When you send the success message. So you can use the below code in your controller:

return Redirect::to("/")->withSuccess('Success message');

The success flash message will look like:

2 – Error Flash Message

If you want to display an error message in your blade views, so you can add the below code in your blade file:

@if(Session::has('fail'))
    <div class="alert alert-danger">
       {{Session::get('fail')}}
    </div>
@endif

The second thing, add the below code in your controller, where you want to send the warning message:

 return Redirect::to("/")->withFail('Error message');

The error flash message will look like:

3 – Warning Flash Message

To display an warning message in your blade views, so you can add the below code in your blade file:

@if(Session::has('warning'))
    <div class="alert alert-danger">
       {{Session::get('warning')}}
    </div>
@endif

The second thing, add the below code in your controller, where you want to send the error message:

return Redirect::to("/")-->with('warning',"Don't Open this link");

The warning flash message will look like:

4 – Info Flash Message

To display an info message in your blade views, so you can add the below code in your blade file:

@if(Session::has('info'))
    <div class="alert alert-danger">
       {{Session::get('info')}}
    </div>
@endif

The second thing, add the below code in your controller, where you want to send the info message:

return Redirect::to("/")-->with('info',"Don't Open this link");

The Info flash message will look like:

Now, you will learn how to use sweet alert message in your laravel application. So, first of all, you need to include sweet alert CDN or sweet alert js library in your laravel application:

5 – Sweet Alert Flash Success Message

If you want to show a success message with sweet alert. So you can use the below code in your blade view files:

   @if(Session::has('success'))
     <script type="text/javascript">
        swal({
            title:'Success!',
            text:"{{Session::get('success')}}",
            timer:5000,
            type:'success'
        }).then((value) => {
          //location.reload();
        }).catch(swal.noop);
    </script>
    @endif

If you use the above code for a success message, this message automatically hides after 5 seconds.

The sweet alert-success message will look like:

sweet alert success flash message

6 – Sweet Alert Flash Error Message

If you want to show an error message with a sweet alert. So you can use the below code in your blade view files:

   @if(Session::has('fail'))
   <script type="text/javascript">
      swal({
          title:'Oops!',
          text:"{{Session::get('fail')}}",
          type:'error',
          timer:5000
      }).then((value) => {
        //location.reload();
      }).catch(swal.noop);
  </script>
  @endif

If you use the above code for an error message, this message automatically hides after 5 seconds.

The sweet alert error message will look like:

sweet alert  flash error message

Conclusion

Laravel 9 flash message tutorial, you have learned how to implement flash messages in laravel 9 applications.

Recommended Laravel Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

One reply to Laravel 9 Flash Message Example

  1. thanks a lot of information keren

Leave a Reply

Your email address will not be published. Required fields are marked *