Laravel 9 Toastr Notifications Example

    In this tutorial, I will show you laravel 9 toastr notifications example. Using toastr.js you can display a success message, warning message, and error with the help of a session in laravel 9. So, in this post, I will show you how to add toastr notifications in laravel 9 and how to add custom messages in toastr notification.

    There are many types of notification available to display different messages in laravel 9 or PHP like display messages using bootstrap modal, simple pop-up notification using jquery, display notification using flash message, and toastr message notification. Also, you can customize as per your requirements like a progress bar, close button, the timing of notification showing.

    So, let's see laravel 9 toastr notifications, how to add toastr notifications in laravel 9.

    First, you need to add bootstrap CSS, toastr notification jquery, toastr CSS, and toastr js in your main view blade file, I have added below CDN in <head> tag.

<head>
    <title>Laravel 9 Toastr Notification Example - Websolutionstuff</title>

    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0- 
     alpha/css/bootstrap.css" rel="stylesheet">
	
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

	<link rel="stylesheet" type="text/css" 
     href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
	
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
</head>
Read Also : How To Send E-mail Using Queue In Laravel 9

    Then after we will add different toastr messages in the script tag like below.

<script>
  @if(Session::has('message'))
  toastr.options =
  {
  	"closeButton" : true,
  	"progressBar" : true
  }
  		toastr.success("{{ session('message') }}");
  @endif

  @if(Session::has('error'))
  toastr.options =
  {
  	"closeButton" : true,
  	"progressBar" : true
  }
  		toastr.error("{{ session('error') }}");
  @endif

  @if(Session::has('info'))
  toastr.options =
  {
  	"closeButton" : true,
  	"progressBar" : true
  }
  		toastr.info("{{ session('info') }}");
  @endif

  @if(Session::has('warning'))
  toastr.options =
  {
  	"closeButton" : true,
  	"progressBar" : true
  }
  		toastr.warning("{{ session('warning') }}");
  @endif
</script>
Read Also : How To Integrate Paypal Payment Gateway In Laravel

    Also, you can customize the toastr notification like the below options example.

toastr.options = {
  "closeButton": false,
  "debug": false,
  "newestOnTop": false,
  "progressBar": true,
  "positionClass": "toast-top-right",
  "preventDuplicates": true,
  "onclick": null,
  "showDuration": "300",
  "hideDuration": "1000",
  "timeOut": "5000",
  "extendedTimeOut": "1000",
  "showEasing": "swing",
  "hideEasing": "linear",
  "showMethod": "fadeIn",
  "hideMethod": "fadeOut"
}

    After that, we need to display messages in the view file using a redirect URL in the controller. So, we need to add some code in the controller also. So, copy the below code in your controller.

return redirect()->route('your route name')->with('message','Data added Successfully');

return redirect()->route('your route name')->with('error','Data Deleted');

return redirect()->route('your route name')->with('Warning','Are you sure you want to delete? ');

return redirect()->route('your route name')->with('info','This is xyz information');

    So, we are done with our code part for toastr notifications example in laravel 9.

    You might also like :

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan