Laravel 10 Flash Message Example

Laravel 10 Flash Message Example

Laravel 10 shows a flash message example; This tutorial will provide you a complete guide on how to show flash messages with redirect in Laravel 10 app.

Flash messages are very important things in any Laravel 10 web or app. Alert messages are considered valuable from a user experience perspective. Sometimes, you need to use various types of flash message notification like alert-success, alert danger, alert info, alert warning messages with bootstrap in Laravel 10 app. So, here, you will learn the various method to show or display flash error, warning, info, and success message in Laravel 10 applications.

This Laravel 10 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.

Flash Messages in Laravel 10

  • 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 10 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 10 flash message tutorial, you have learned how to implement flash messages in Laravel 10 applications.

Recommended Laravel Posts

Recommended:-Laravel Try Catch

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.

Leave a Reply

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