Laravel 8.x Toastr Notifications Example Tutorial

 

Laravel 8.x Toastr Notifications Example Tutorial



Toastr is an awesome JavaScript library that helps to display a message in the form of notification, the message could be the type of info, success, warning, or error. When a user signs up on your website or performs an action that needs a notification. A good option is to use the ToastrJS library. 

But in this tutorial, I will use this packages  to complete Toastr notification system in our Laravel app. You can read this documentation also. I think it is pretty awesome and simple also easy to customize visualization. 

In this tutorial, I will show you how to use the toast message in Laravel 8. Toastr is an awesome plugin to show awesome messages to users. So let's start with how to add toastr js in Laravel application.


Step 1 : Install toastr

Run the below command to install Toastr notification package

composer require brian2694/laravel-toastr
PHP

 

That's it! The package is auto-discovered on 5.5 and up!

Laravel <= 5.4

Add the service provider to config/app.php

Brian2694\Toastr\ToastrServiceProvider::class,
PHP

 

Optionally include the Facade in config/app.php if you'd like.

'Toastr'  => Brian2694\Toastr\Facades\Toastr::class,
PHP

 

Options

You can set custom options for customizing your notification view. Run:

php artisan vendor:publish
PHP

 

to publish the config file for toastr. You can see toastr's documentation to custom your need.

Dependencies

jQuery toast, you need to add CSS and js to your HTML. So now add the following code to your master template file before closing the body tag.

 <script src="http://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/toastr.js/latest/js/toastr.min.js"></script>
{!! Toastr::message() !!}
PHP

 

and before closing the head tag, paste the following link.

 <link rel="stylesheet" href="http://cdn.bootcss.com/toastr.js/latest/css/toastr.min.css"> 
PHP

 

now add the below code to your controller like below.

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;

class PostController extends Controller
{
    
  
     public function store(Request $request)
    {   
       //Your code goes here
       
      Toastr::success('Post added successfully :)','Success');
    } 
  
}

Post a Comment

Previous Post Next Post