Laravel 8 Flash Message Example Tutorial

Laravel 8 Flash Message Example Tutorial

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

Flash messages are very important things in any laravel 8 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 8 app. So, here, you will learn the various method to show or display flash error, warning, info, and success message in laravel 8 applications.

This laravel 8 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 8

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

Recommended Laravel Posts

Recommended:-Laravel Try Catch

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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