Notification custom action

Is it possible to add a custom action to the notification?
13 Replies
LancelotGamer
LancelotGamerOP2y ago
I read the docs In the pic which I sent, The approve button dose nothing I want to add an action to the button
Dennis Koch
Dennis Koch2y ago
Share some code 🤷🏼‍♂️ We can't read screenshots
LancelotGamer
LancelotGamerOP2y ago
public function toDatabase(): array
{
return FilamentNotification::make()
->title('message')
->actions([
Action::make('approve')
->button()
])
->getDatabaseMessage();
}
public function toDatabase(): array
{
return FilamentNotification::make()
->title('message')
->actions([
Action::make('approve')
->button()
])
->getDatabaseMessage();
}
toeknee
toeknee2y ago
what are you wanting the Action to do? I don't see any ->action() within there that is going to run a function.
Dennis Koch
Dennis Koch2y ago
You just added an empty action, so how to you expect it to do anything?
LancelotGamer
LancelotGamerOP2y ago
public function toDatabase(): array
{
return FilamentNotification::make()
->title('Withdraw from: ' . User::find($this->userId)->email . '(Amount: ' . $this->amount . '$' . ')')
->actions([
Action::make('approve')
->button()
->action(function () {
$user = User::find($this->userId);
$user->balance -= $this->amount;
$user->save();
})
])
->getDatabaseMessage();
}
public function toDatabase(): array
{
return FilamentNotification::make()
->title('Withdraw from: ' . User::find($this->userId)->email . '(Amount: ' . $this->amount . '$' . ')')
->actions([
Action::make('approve')
->button()
->action(function () {
$user = User::find($this->userId);
$user->balance -= $this->amount;
$user->save();
})
])
->getDatabaseMessage();
}
When I add the action function to the button, An error accrue Method Filament\Notifications\Actions\Action::action does not exist
Dennis Koch
Dennis Koch2y ago
Ah right. You can't execute stuff from Notification actions directly. You can emit or redirect with url
LancelotGamer
LancelotGamerOP2y ago
Ok After emitting an event can I listen to it using a Laravel listener class Or I am missing somthing
Dennis Koch
Dennis Koch2y ago
Yeah, that should work Livewire listener
LancelotGamer
LancelotGamerOP2y ago
Thank you so much I want to send the event to a Livewire listener using emitTo but this error accrue Method Filament\Notifications\Actions\Action::emitTo does not exist
Dennis Koch
Dennis Koch2y ago
Are you on a recent version?
digitall_it
digitall_it2y ago
Follow this steps: 1. php artisan make:livewire GlobalEventsListener 2. open App\Providers\AppServiceProvider and add this code:
Filament::serving(function () {
Livewire::component('global-events-listener', GlobalEventsListener::class);
Filament::registerRenderHook(
'user-menu.start',
fn (): string => Blade::render("@livewire('global-events-listener')")
);
});
Filament::serving(function () {
Livewire::component('global-events-listener', GlobalEventsListener::class);
Filament::registerRenderHook(
'user-menu.start',
fn (): string => Blade::render("@livewire('global-events-listener')")
);
});
3. open App\Http\Livewire\GlobalEventsListener and add this code:
protected $listeners = [
'myEventName'
];

public function myEventName($data) {
// handle the action here
}
protected $listeners = [
'myEventName'
];

public function myEventName($data) {
// handle the action here
}
4. in your notification's action, emit the event like this ->emit('myEventName', ['my_eloquent_model_id' => $myId]) 5. Profit.
Want results from more Discord servers?
Add your server