Guide to Implement Database Notifications in a Filament App

Guide to Implement Database Notifications in a Filament App

Introduction

Implementing Database Notifications in a Laravel/Filament app is really simple, but the official docs do not contain complete info about it. The information is scattered on different pages, so in this blog, we'll be looking at implementing database notifications in a Filament-based application.


Configuration

Enabling Database Notifications

The first that we need to do is enable database notifications in our Filament Panel provider, the provider here denotes the panel for which we want to enable the database notification.

In this case, we'll be implementing it for the default AdminPanelProvider and enable database notifications by passing true Boolean value to databaseNotifications method like so:

<?php

namespace App\Providers\Filament;

use Filament\Pages;
use Filament\Panel;
use Filament\Widgets;
use Filament\PanelProvider;
use Filament\Http\Middleware\Authenticate;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            // This enables database notification for the admin panel
            ->databaseNotifications(true)
            ->id('admin')
            ->path('admin')
            ->login()
            ->maxContentWidth('full')
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
            ->pages([
                Pages\Dashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
            ->widgets([
                Widgets\AccountWidget::class,
                Widgets\FilamentInfoWidget::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ]);
    }
}

Defining the Migration file

Once this is enabled, the next step is to define the migration for the notifications table which can be done with the below command

 php artisan notifications:table

This command will create the migration file, let's go ahead and run the migrations with php artisan migrate command.

Sending the Database Notification

The final step is to actually send the database notification to the specific user.

For demo purposes, we'll grab a User from the database and send the notification to that user, and the notification can be sent in multiple ways. One of the ways is by using the Notification class provided by the Filament and sending it accordingly.

use App\Models\User;
use Filament\Notifications\Notification;

$user = User::find(1);

Notification::make()
    ->title('A database notification has been created')
    ->sendToDatabase($user);

Conclusion

As you can see here, implementing database notifications in a Filament app is really easy, as most of the things are already configured for us.

But you might wonder, where to put the above shown code, since I haven't shown a proper use case here.

These notifications can be sent whenever any action is performed, or any task is assigned to another user. So, they can be typically used inside an action or a Model Observer.


🔥 New Course Alert
★★★★½4.6/5

Filament Admin Panel Course for Laravel (Updated for V3)

Learn to build Admin Panels using FilamentPHP practically by building multiple projects along the way.

30-Day Money Back
1000+ Students
Limited Time
80% OFF
Enroll Now
A
Athanasios A.
★★★★★

Been on Laravel development with blade only for a few years now and decided to give Filament a go. What a brilliant surprise to find this tutorial ! Tapan is an excellent tutor he goes into all the corners of the development labyrinth that we all find ourselves into when getting onboard with a new tech and he manages to give us all everything we would need to deliver a fully-pledged app. I dont think once could ask for anything more. The tutorial spoon feeds us with concise information and his calm voice really helps. Five stars for me, i have a very productive weekend and now I will be all heads in development with Filament. So excited! Thank you Tapan !