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:
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:
Conclusion
Laravel 9 flash message tutorial, you have learned how to implement flash messages in laravel 9 applications.
thanks a lot of information keren