In this tutorial you will learn about the Laravel 7/6 Flash Message Example and its application with practical example.
In this Laravel Flash Message Example tutorial I will show you how to create or implement flash message in laravel 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 7/6 Flash Message Example
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.
Laravel Flash Message
In this tutorial we will learn to following types of flash messages
- Laravel Success Flash Message
- Laravel Error Flash Message
- Laravel Flash Success With Sweet alert
- Laravel Flash Error With Sweet alert
Laravel Success Flash Message
Below is example to display success message:
1 2 3 4 5 |
@if(Session::has('success')) <div class="alert alert-success"> {{Session::get('success')}} </div> @endif |
Below is example to send and display success message on redirect method:
1 |
return Redirect::to("/")->withSuccess('Success message'); |
Laravel Error Flash Message
Below is example to display error message:
1 2 3 4 5 |
@if(Session::has('fail')) <div class="alert alert-danger"> {{Session::get('fail')}} </div> @endif |
Below is example to send and display error message on redirect method:
1 |
return Redirect::to("/")->withFail('Error message'); |
Laravel Flash Success With Sweet Alert
If you want to show a success message with sweet alert, use the following code in your blade files:
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 |
Laravel Flash Error With Sweet Alert
If you want to show an error message with a sweet alert use the following code in your blade files:
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 |