In this tutorial you will learn about the Laravel 8 Flash Message Example Tutorial and its application with practical example.
In this Laravel 8 Flash Message Example Tutorial I will show you how to create or implement flash message in laravel 8 application. In this tutorial you will learn to implement flash message in laravel. While working with laravel application we come to situations where we want to show/display various flash success and error messages. In this example we will be using sweet alert js to display various type of flash messages or notifications.
Laravel 8 Flash Message Example Tutorial
In this step by step guide I will demonstrate you how you can implement success or error messages with sweet alert js in your laravel application.
Flash Messages in Laravel 8
In this tutorial we will learn to following types of flash messages
- 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
Below is example to display success flash message:
1 2 3 4 5 |
@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:
1 |
return Redirect::to("/")->withSuccess('Success message'); |
The success flash message will look like:
2 – Error Flash Message
Below is example to display error flash message:
1 2 3 4 5 |
@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:
1 |
return Redirect::to("/")->withFail('Error message'); |
3 – Warning Flash Message
Below is example to display warning flash message:
1 2 3 4 5 |
@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:
1 |
return Redirect::to("/")-->with('warning',"Don't Open this link"); |
4 – Info Flash Message
Below is example to display info flash message:
1 2 3 4 5 |
@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:\
1 |
return Redirect::to("/")-->with('info',"Don't Open this link"); |
5 – Sweet Alert Flash Success Message
Below is example to sweet alert flash success message:
1 2 3 4 5 6 7 8 9 10 11 12 |
@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.
6 – Sweet Alert Flash Error Message
Below is example to sweet alert flash error message:
1 2 3 4 5 6 7 8 9 10 11 12 |
@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.