Laravel Toastr Notifications Example

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello Dev
Now this is a simple article demonstrates of laravel livewire toastr notifications.So if you want to know about laravel livewire toastr notifications alert . Then you are in a right place.Now i’m going to show you about laravel livewire toastr.js example. So you can learn toastr notification in laravel livewire. Now let’s see bellow example laravel livewire toastr notifications.

So i will give you very simple example of how to implement toastr notification alert using laravel livewire. It’s really very simple step for creating toaster notification with livewire. You can use with laravel 6, laravel 7 and laravel 8 version.

So, let’s follow bellow step and you will get bellow layout:

Step 1: Install Laravel

First of all we need a fresh Laravel version application by using bellow command. So open your terminal OR command prompt and run bellow command:

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

Step 2: Install Livewire

Now in this step, we need to install livewire to our laravel application. So we can using it by bellow command:

composer require livewire/livewire

Read Also: How Build Admin Panel with Laravel 5.8?

Step 3: Create Component

Now here we will create livewire component by using their command. So run bellow command to create select2 component.

php artisan make:livewire notificationDemo

Now they created fies on both path:

app/Http/Livewire/NotificationDemo.php
resources/views/livewire/notification-demo.blade.php

Now both file we will update as bellow for our contact us form.

app/Http/Livewire/NotificationDemo.php

<?php
  
namespace App\Http\Livewire;
  
use Livewire\Component;
  
class NotificationDemo extends Component
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function render()
    {
        return view('livewire.notification-demo')->extends('layouts.app');
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function alertSuccess()
    {
        $this->dispatchBrowserEvent('alert', 
                ['type' => 'success',  'message' => 'User Created Successfully!']);
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function alertError()
    {
        $this->dispatchBrowserEvent('alert', 
                ['type' => 'error',  'message' => 'Something is Wrong!']);
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function alertInfo()
    {
        $this->dispatchBrowserEvent('alert', 
                ['type' => 'info',  'message' => 'Going Well!']);
    }
}

resources/views/livewire/notification-demo.blade.php

<div>
    <h1>Laravel Livewire Notification Example - Codingspoint.com</h1>
      
    <button type="button" wire:click="alertSuccess" class="btn btn-success">Success Alert</button>
    <button type="button" wire:click="alertError" class="btn btn-danger">Error Alert</button>
    <button type="button" wire:click="alertInfo" class="btn btn-info">Info Alert</button>
</div>

Step 4: Create Route

Now we need to create one route for calling our example, so let’s add new route to web.php file as bellow:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
 
use App\Http\Livewire\NotificationDemo;
  
/*
|--------------------------------------------------------------------------
| 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('notification', NotificationDemo::class);

Step 5: Create View File

Here, now we will create blade file for call form route. In this file we will use @livewireStyles, @livewireScripts. so let’s add it.

resources/views/layouts/app.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Livewire Example - Codingspoint.com</title>
    @livewireStyles
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
</head>
<body>
    
<div class="container">
    @yield('content')
</div>
    
</body>
  
@livewireScripts
  
<script>
window.addEventListener('alert', event => { 
             toastr[event.detail.type](event.detail.message, 
             event.detail.title ?? ''), toastr.options = {
                    "closeButton": true,
                    "progressBar": true,
                }
            });
</script>
  
</html>

Now you can run using bellow command:

php artisan serve

Open bellow URL:

Read Also: How to use laravel livewire select2 dropdown?

localhost:8000/notification

I hope it can help you…

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →

One Comment on “Laravel Toastr Notifications Example”

Leave a Reply

Your email address will not be published. Required fields are marked *

six + ten =