How to redirect after delete on DeleteAction using Laravel Filament v3?
To redirect after deleting a resource using Laravel Filament v3, you can follow these steps:
- Create a method in your Filament Resource class to handle the delete request and redirect after deletion.
protected function delete(Request $request): RedirectResponse
{
$record = $this->getRecord();
$record->delete();
return Redirect::route('filament.resources.'.$this->getModel() . '.index');
}
In the above code, $this->getRecord()
returns the current record being deleted. We call the delete()
method on the record to delete it from the database. After deletion, we redirect the user to the index page of the resource using Redirect::route()
.
- Override the
delete()
method in theDeleteAction
class to call thedelete()
method in your resource class.
protected function delete(Request $request): void
{
$this->callAction('delete', $request);
}
In the above code, we call the delete()
method in the resource class by using $this->callAction()
.
- Register the
DeleteAction
with your resource class.
protected function getActions(): array
{
return [
// ...
DeleteAction::make(),
];
}
In the above code, we register the DeleteAction
in the getActions()
method of the resource class.
Now, when you delete a record using the delete button in Filament, it will call the delete()
method in your resource class and redirect the user to the index page after deletion.