Laravel 8 Toastr Notifications Example
Tags: laravel, laravel8, toastr, notification
Published: 2022-01-28 03:34:48

In this tutorial, I will show you laravel 8 toastr notifications example. Using toastr.js you can display a success message, warning message and error with the help of a session in laravel 8. 

So, in this post, I will show you how to add toastr notifications in laravel 8 and how to add custom messages in toastr.

There are many types of notification available to display different messages in laravel 8 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.

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

```
<head>
    <title>Laravel 8 Toastr Notification Example - Techsolutionstuff</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 differents toastr message in 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>
```

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 8.


![laravel_8_toastr_notifications_example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmijdtw8d1ntetcaj1x1.png)

---

You might also like :

- [Read Also : Laravel 8 Google Recaptcha Example](https://techsolutionstuff.com/post/laravel-8-google-recaptcha-example)

- [Read Also : Laravel 6 CRUD Tutorial with Example](https://techsolutionstuff.com/post/laravel-6-crud-tutorial-with-example)

- [Read Also : How To Install TinyMCE Editor In Laravel](https://techsolutionstuff.com/post/how-to-install-tinymce-editor-in-laravel)