Action failed notification.

I have to delete the record from the table using table delete actions, Sometimes there is an issue with delete operation and throw the exception, I want to show that exception message on failed notification. How can I do that in filament.
Solution:
@Karthick K Thanks for the idea. Did you say like this Service Provider: ```php public function register(): void {...
Jump to solution
6 Replies
Asmit Nepali
Asmit Nepali3w ago
Any one can help
Chrispian
Chrispian3w ago
This is UNTESTED but should get you in the ballpark I would think.
Action::make('delete')
->requiresConfirmation()
->action(function (YourModel $record) {
try {
$record->delete();
} catch (\Exception $e) {
Notification::make('exception', ['message' => $e->getMessage()])->send();
$this->halt();
}
})
Action::make('delete')
->requiresConfirmation()
->action(function (YourModel $record) {
try {
$record->delete();
} catch (\Exception $e) {
Notification::make('exception', ['message' => $e->getMessage()])->send();
$this->halt();
}
})
See if you can get the exception that way and then you can make the notification nicer if needed.
Asmit Nepali
Asmit Nepali3w ago
@Chrispian I used this but I want it from global level and try to configure like
DeleteAction::configureUsing(function (DeleteAction $deleteAction) {
return $deleteAction->action(function ($record) {
try {
$record->delete();
Notification::make()
->success()
->title('Record deleted successfully!')
->send();
} catch (\Exception $e) {
Notification::make()
->danger()
->title($e->getMessage())
->duration(5000)
->send();
}
});
});
DeleteAction::configureUsing(function (DeleteAction $deleteAction) {
return $deleteAction->action(function ($record) {
try {
$record->delete();
Notification::make()
->success()
->title('Record deleted successfully!')
->send();
} catch (\Exception $e) {
Notification::make()
->danger()
->title($e->getMessage())
->duration(5000)
->send();
}
});
});
But it won't work.
Karthick K
Karthick K3w ago
What you can you do it create a extended class of the delete action and then update the code with the notification. Now bind the new delete action to filament delete action. Now it is implemented global level
Solution
Asmit Nepali
Asmit Nepali3w ago
@Karthick K Thanks for the idea. Did you say like this Service Provider:
public function register(): void
{
$this->app->bind(DeleteAction::class, TableDeleteAction::class);
}
public function register(): void
{
$this->app->bind(DeleteAction::class, TableDeleteAction::class);
}
TableDeleteAction:
class TableDeleteAction extends DeleteAction
{
protected function setUp(): void
{
parent::setUp();

$this->action(function ($record) {
try {
$record->delete();
Notification::make()
->success()
->title('Record deleted successfully!')
->send();
} catch (ManuallyFailedException $e) {
Notification::make()
->danger()
->title($e->getMessage())
->duration(5000)
->send();
}
});
}
class TableDeleteAction extends DeleteAction
{
protected function setUp(): void
{
parent::setUp();

$this->action(function ($record) {
try {
$record->delete();
Notification::make()
->success()
->title('Record deleted successfully!')
->send();
} catch (ManuallyFailedException $e) {
Notification::make()
->danger()
->title($e->getMessage())
->duration(5000)
->send();
}
});
}
Karthick K
Karthick K3w ago
Yes exactly. It should work