F
Filament7mo ago
Rad

Custom Database Notification Model

I wanted to add some actions to each database Notification and as is don’t see an option where that is possible. Is ‘extending’ from BaseNotification the right way to go ?
2 Replies
LeandroFerreira
LeandroFerreira7mo ago
Do you mean this?
->actions([
\Filament\Notifications\Actions\Action::make('customAction')
...
])
->actions([
\Filament\Notifications\Actions\Action::make('customAction')
...
])
Povilas K
Povilas K7mo ago
@Rad not sure if I'm not too late, but we experimented and came up with this solution. Create your own class extending BaseNotification, then bind it in the provider, and then use it where needed. app/Filament/Notification.php:
namespace App\Filament;

use Closure;
use Illuminate\Support\Arr;
use Filament\Notifications\Actions\Action;
use Filament\Notifications\Actions\ActionGroup;
use Filament\Notifications\Notification as BaseNotification;

class Notification extends BaseNotification
{
/**
* @param array<Action | ActionGroup> | ActionGroup | Closure $actions
*/
public function actions(array | ActionGroup | Closure $actions): static
{
$this->actions = Arr::prepend(
$actions,
Action::make('goToDashboard')
->label('Go to Dashboard')
->url(fn() => route('filament.admin.pages.dashboard'))
);

return $this;
}
}
namespace App\Filament;

use Closure;
use Illuminate\Support\Arr;
use Filament\Notifications\Actions\Action;
use Filament\Notifications\Actions\ActionGroup;
use Filament\Notifications\Notification as BaseNotification;

class Notification extends BaseNotification
{
/**
* @param array<Action | ActionGroup> | ActionGroup | Closure $actions
*/
public function actions(array | ActionGroup | Closure $actions): static
{
$this->actions = Arr::prepend(
$actions,
Action::make('goToDashboard')
->label('Go to Dashboard')
->url(fn() => route('filament.admin.pages.dashboard'))
);

return $this;
}
}
app/Providers/AppServiceProvider.php:
use App\Filament\Notification;
use Filament\Notifications\Notification as BaseNotification;

public function register(): void
{
$this->app->bind(BaseNotification::class, Notification::class);
}
use App\Filament\Notification;
use Filament\Notifications\Notification as BaseNotification;

public function register(): void
{
$this->app->bind(BaseNotification::class, Notification::class);
}
Then, in the Create pages or wherever you need, call your Notification class: app/Filament/Resources/PostResource/Pages/CreatePost.php:
use App\Filament\Notification;

// ...

public function afterCreate(): void
{
Notification::make()
->title('Post created')
->sendToDatabase(auth()->user());
}
use App\Filament\Notification;

// ...

public function afterCreate(): void
{
Notification::make()
->title('Post created')
->sendToDatabase(auth()->user());
}
Want results from more Discord servers?
Add your server