In this article. Today, we will discuss you how to make toastr notification example in Laravel 9(Laravel 9 Toastr Notifications Example Tutorial). There are many different types of notification available but here we are using toastr notification with bootstrap. so we need to put toastr CDN and bootstrap CDN in the head tag. so you can see our laravel 9 toastr notifications example.

Overview

Step 1: Install Laravel 9

Step 2: Create Controller

Step 3: Create Blade Files

Step 4: Create Route in web.php file

Step 5: Run Our Laravel Application

Step 1: Install Laravel 9

We are going to install laravel 9, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

composer create-project --prefer-dist laravel/laravel laravel9_notification

Step 2: Create Controller

Here below command help to create the controller.

php artisan make:controller UserController

UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
		return view('user');
    }
	
	public function data(){
		return redirect()->route('user')->with('message','Data added Successfully');
		 
		//return redirect()->route('user')->with('error','Data Deleted');
		 
		//return redirect()->route('user')->with('Warning','Are you sure you want to delete? ');
		 
		//return redirect()->route('user')->with('info','This is xyz information');

	}
}

Step 3: Create Blade Files

First, we will create the user.blade.php file and then paste the following code.

user.blade.php

<!doctype html>
<html lang="en">

<head>
    <title>Laravel 9 Toastr Notifications Example Tutorial - xpertphp</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
	<script>
	$(document).ready(function() {
		@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>

</head>

<body>
    <div class="container my-5">
    </div>
</body>
</html>

Step 4: Create Route in web.php file

We have to need put the below route in the routes/web.php file.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
 
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/user',[UserController::class, 'index'])->name('user');
Route::get('/user-data',[UserController::class, 'data']);

Step 5: Run Our Laravel Application
We can start the server and run this example using the below command.

php artisan serve

Now we will run our example using the below Url in the browser.

http://127.0.0.1:8000/user-data