How To Add Toastr Notification In Laravel

Websolutionstuff | May-29-2020 | Categories : Laravel PHP jQuery CSS Bootstrap

In this tutorial, I will show you how to add toastr notification in laravel application. There are many types of notifications available to display different messages in laravel or PHP like simple pop-up notifications using jquery, display messages using bootstrap modal, display notifications using flash message, and toastr message notifications.

So, let's see how to add toastr notification in laravel 6/7/8, toastr notification example in laravel 6/7/8, how to add a custom message in toastr notification, how to use toastr in laravel, how to install toastr in laravel 8, toast notification jquery.

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

<head>
    <title>Laravel Toastr Notification Example - websolutionstuff.com</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 need to 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>

 

 

After that, we need to display messages in the view file using the redirect URL in the controller. So, we need to add some code to 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, I hope you will be successfully implementing this code and displaying the different messages in your laravel applications.

how to add toastr notification in laravel

 


You might also like :

Recommended Post
Featured Post
The Best Laravel Tips & Tricks for Migrations
The Best Laravel Tips & Tricks...

Migrations are an essential part of any Laravel project, allowing developers to easily manage and update their database...

Read More

Oct-23-2023

Laravel 11 ChartJS Line Chart Example
Laravel 11 ChartJS Line Chart...

Hello developer! In this article, we'll see how to create a dynamic line chart in laravel 11 using chart js. Yo...

Read More

May-10-2024

Laravel 8 Has Many Through Relationship Example
Laravel 8 Has Many Through Rel...

In this example we will see laravel 8 has many through relationship example. hasManyThrough relationship difficult to un...

Read More

Nov-17-2021

How To Generate QRcode In Laravel
How To Generate QRcode In Lara...

In this example, I will give information about how to generate QR code in laravel. As per the current trend, many w...

Read More

Jun-01-2020