A Filament plugin that provides elegant handling of "record not found" exceptions. When users try to view or edit a non-existent record in Filament, they'll be redirected to the resource's index page with a customizable notification instead of seeing an error page.
- Seamlessly redirects users when accessing non-existent records
- Displays customizable notifications explaining what happened
- Custom redirect destinations based on context
- Supports all resource pages using the
InteractsWithRecord
trait - Fully customizable notification appearance and behavior
- Granular control over which resources and pages are handled
- Simple integration with any Filament panel
You can install the package via composer:
composer require padmission/missing-record-redirect
Add the plugin to your Filament panel in a panel provider:
use Padmission\MissingRecordRedirect\MissingRecordRedirectPlugin;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(
MissingRecordRedirectPlugin::make()
);
}
The plugin will now handle missing record exceptions by redirecting users to the resource index page with a notification.
You can customize the notification that is shown to users:
// Set simple text properties
MissingRecordRedirectPlugin::make()
->notificationTitle('Record Not Available')
->notificationBody('The record you were trying to access does not exist.')
// Or use the notification callback for advanced customization
MissingRecordRedirectPlugin::make()
->notification(function (Notification $notification, NotificationContext $context): Notification {
$action = $context->isEditPage() ? 'edit' : 'view';
return $notification
->title('Record Not Found')
->body("The {$context->getResource()::getModelLabel()} you were trying to {$action} has been deleted or does not exist.")
->warning()
->persistent();
})
Change where users are redirected when a record is not found:
// Set a static URL
MissingRecordRedirectPlugin::make()
->redirectUrl('/admin/dashboard')
// Or use a callback for dynamic URLs based on context
MissingRecordRedirectPlugin::make()
->redirectUrl(function (NotificationContext $context) {
// For edit pages, redirect to create page
if ($context->isEditPage()) {
return $context->getResource()::getUrl('create');
}
// Default to resource list
return $context->getResource()::getUrl();
})
You can exclude specific resources, models, or page types from being handled by the plugin:
MissingRecordRedirectPlugin::make()
// Exclude specific resources
->excludeResources(
App\Filament\Resources\UserResource::class,
App\Filament\Resources\ProductResource::class
)
// Exclude specific models
->excludeModels(
App\Models\Setting::class,
App\Models\SystemLog::class
)
// Exclude specific resource page classes
->excludePages(
App\Filament\Resources\PostResource\Pages\EditPost::class
)
For complete control over exception handling:
MissingRecordRedirectPlugin::make()
->handleException(function (NotFoundHttpException $exception, Request $request) {
// Custom exception handling logic
// Return a RedirectResponse or null
})
The NotificationContext
object provides access to information about the current request:
Method | Description |
---|---|
getResource() |
Get the resource class name |
getPage() |
Get the page instance |
getRequest() |
Get the current request |
getException() |
Get the ModelNotFoundException |
isViewPage() |
Check if this is a view page |
isEditPage() |
Check if this is an edit page |
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.