How to Flash Message in Laravel Example

How to Flash Message in Laravel Example

Today I will share with you how to Flash Message in Laravel Example without package in the Laravel program. In this example I did not use the flash message pack. We can do it by simply following these flash message lessons in laravel 6, laravel 7 and laravel 8.

Sometimes, you require to use various types of flash message notification such as alert-success, alert danger, alert info, alert warning messages with bootstrap in laravel 8 app. So, here, you will learn the all sorts method to display or show flash error, warning, info, and success message in laravel 8 applications.

Flash messages are required in the laravel app. Because thus we can provide a change for any complete progress, error, warning. and redirect with a message of information. In this example we use bootstrap the flash alert format thus becoming a good configuration.

In this tutorial, we added a number of flash message delivery methods such as effective message redirection, error message redirection, redirect message alert, and redirect message information. Also, use a warning format with bootstrap thus showing a good design.

How to Flash Message in Laravel Example

Here, you will learn the 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
Read Also :  Full Text Search in Laravel Example

Success Flash Message in Laravel

First of all, we require to add the follow 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 follow code in your controller.

return Redirect::to("/")->withSuccess('Success message');

Output : success flash message will look like.

Success Flash Message in Laravel

Error Flash Message in Laravel

If you want to display an error message in your blade views. So you can add the follow code in your blade file.

@if(Session::has('fail'))
     <div class="alert alert-danger">
        {{Session::get('fail')}}
     </div>
 @endif

The second thing, When you send the error message. So you can use the follow code in your controller.

 return Redirect::to("/")->withFail('Error message');

Output : error flash message will look like.

Error Flash Message in Laravel

Warning Flash Message in Laravel

If you want to display an warning message in your blade views. So you can add the follow code in your blade file.

@if(Session::has('warning'))  
   <div class="alert alert-danger">  
      {{Session::get('warning')}}  
   </div>
@endif

The second thing, When you send the warning message. So you can use the follow code in your controller.

return Redirect::to("/")->with('warning',"Don't Open this link");

Output : warning flash message will look like.

Read Also :  Laravel Arr wrap() function Example

warning flash message

Info Flash Message in Laravel

If you want to display an info message in your blade views. So you can add the follow code in your blade file.

@if(Session::has('info'))
   <div class="alert alert-danger">
       {{Session::get('info')}}  
   </div>
 @endif

The second thing, When you send the info message. So you can use the follow code in your controller.

return Redirect::to("/")->with('info',"Don't Open this link");

Output : info flash message will look like.

Info Flash Message in Laravel

Sweet Alert Success Message

If you wish to show a success message with sweet alert. So you can use the follow code in your blade view file.

@if(Session::has('success'))
 <script type="text/javascript">    
  swal({    
      title:'Success!',      
      text:"{{Session::get('success')}}",   
      timer:10000,     
     type:'success'     
 }).then((value) => {       
 //location.reload();     
 }).catch(swal.noop);
</script>
@endif

If you use the above code for a success message. This message automatically hide after 10 seconds.

Read Also :  Insert Form Data Into Database using Laravel

Sweet Alert Error Message

If you wish to show a error message with sweet alert. So you can use the follow code in your blade view file.

@if(Session::has('fail'))
  <script type="text/javascript">  
   swal({        
        title:'Oops!',  
        text:"{{Session::get('fail')}}",   
       type:'error',     
       timer:10000   
  }).then((value) => {  
     //location.reload();  
   }).catch(swal.noop);
 </script>
@endif

If you use the above code for a error message. This message automatically hide after 10 seconds.