
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.