How To Use Toastr Notification In Laravel 9

  Jul 2023
  ITSolutionsGuides
  Category: Laravel
How To Use Toastr Notification In Laravel 9

Hi Developers ,

Lets see How To Use Toastr Notification In Laravel 9 Application. Toastr Js is a famous library use to notify the user's about the results of their actions and also any other notifications . Since Toast JS is a user friendly library it allows the user to customize the notification like animation location , animation duration , notification color etc.

Toastr Notification In Laravel is mainly used in the admin panel's example , during login we should notify the user if the login credentials were wrong , wheather the data has been deleted or updated based on the user request etc. We can also use different color notifications based on the notification type wheather it is a success or warning or any information notification.

Add Toast Notification Library

First, we will add bootstrap CSS, toastr notification jquery, toastr CSS, and toastr js in the main view blade file, I have added the below CDN in tag.

<head>
    <title>How To Add Toastr Notifications In Laravel 9 - ITSolutionsGuides</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>

Add Toast Scripts

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>

Options Available In Toast

Also, you can customize the toastr notification with plenty of the options that are provided by the toast notification library 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"
}

Routing

Invoking the Script while you are passing the routes in the controller using the keyword with() the first parameter passes the type of the toast notification(color; example warning:yellow, info:blue etc) and the second parameter in message you want to display

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');

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides