Laravel 9 Toastr Notifications Example

Websolutionstuff | Feb-23-2022 | Categories : Laravel PHP jQuery

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>

 

 

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>

 

 

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 :

Recommended Post
Featured Post
CRUD With Image Upload In Laravel 10 Example
CRUD With Image Upload In Lara...

In this article, we will see crud with image upload in laravel 10 examples. Here, we will learn how to image upload with...

Read More

Mar-27-2023

Laravel 11 Livewire Toastr Notification
Laravel 11 Livewire Toastr Not...

Hello, laravel web developers! In this article, we'll see how to add toastr notification in livewire laravel 11. Her...

Read More

May-31-2024

Multiple ways to Validate Email Addresses in Laravel 10
Multiple ways to Validate Emai...

Hey there! Have you ever wondered how websites ensure that the email addresses you provide are valid? In this article, I...

Read More

Mar-04-2024

Datatables Expand/Collapse Columns
Datatables Expand/Collapse Col...

In this article, we will see how to expand/collapse columns in datatable. The Datatables API has a number of method...

Read More

Jun-05-2022