Laravel Toastr Notifications Example Tutorial

Laravel Toastr Notifications Example Tutorial

Laravel Toastr Notifications Example Tutorial




Toastr is an awesome JavaScript library that helps to display a message in the form of a 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 package to complete the 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 the Laravel application.

Step 1: Install toastr

Run the below command to install the 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 customize your needs.

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.

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');
    } 
}
Reactions

Post a Comment

5 Comments

CAN FEEDBACK

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)

close