A Laravel package that adds elegant next/previous record navigation to your Filament PHP admin panels. Navigate seamlessly between records with intuitive navigation buttons.
Install the package via Composer:
composer require nben/filament-record-nav
Publish the configuration file (optional):
php artisan vendor:publish --tag=filament-record-nav-config
Add the WithRecordNavigation
trait to your ViewRecord
or EditRecord
pages:
<?php namespace App\Filament\Resources\PostResource\Pages; use App\Filament\Resources\PostResource;use Filament\Resources\Pages\ViewRecord; use Nben\FilamentRecordNav\Concerns\WithRecordNavigation; class ViewPost extends ViewRecord{ use WithRecordNavigation; protected static string $resource = PostResource::class;}
Add the navigation actions to your page's action array:
<?php namespace App\Filament\Resources\PostResource\Pages; use App\Filament\Resources\PostResource;use Filament\Resources\Pages\ViewRecord; use Nben\FilamentRecordNav\Actions\NextRecordAction;use Nben\FilamentRecordNav\Actions\PreviousRecordAction; use Nben\FilamentRecordNav\Concerns\WithRecordNavigation; class ViewPost extends ViewRecord{ use WithRecordNavigation; protected static string $resource = PostResource::class; protected function getHeaderActions(): array { return [ PreviousRecordAction::make(), NextRecordAction::make(), // ... your other actions ]; }}
That's it! Your Filament resource pages now have beautiful next/previous navigation buttons.
The package comes with sensible defaults, but you can customize the behavior by publishing and modifying the configuration file:
<?php return [ /* |-------------------------------------------------------------------------- | Default Navigation Column |-------------------------------------------------------------------------- | | This column will be used to determine the order of records | for navigation. Common choices: 'id', 'created_at', 'updated_at' | */ 'order_column' => 'id', /* |-------------------------------------------------------------------------- | Navigation Directions |-------------------------------------------------------------------------- | | Define the sort directions for previous and next navigation | */ 'previous_direction' => 'desc', 'next_direction' => 'asc',];
You can override the navigation methods in your page class for custom behavior:
<?php namespace App\Filament\Resources\PostResource\Pages; use App\Filament\Resources\PostResource;use Filament\Resources\Pages\ViewRecord; use Illuminate\Database\Eloquent\Model;use Nben\FilamentRecordNav\Concerns\WithRecordNavigation; class ViewPost extends ViewRecord{ use WithRecordNavigation; protected static string $resource = PostResource::class; // Custom logic for finding the previous record protected function getPreviousRecord(): ?Model { return $this->getRecord() ->newQuery() ->where('status', 'published') // Only navigate through published posts ->where('created_at', '<', $this->getRecord()->created_at) ->orderBy('created_at', 'desc') ->first(); } // Custom logic for finding the next record protected function getNextRecord(): ?Model { return $this->getRecord() ->newQuery() ->where('status', 'published') // Only navigate through published posts ->where('created_at', '>', $this->getRecord()->created_at) ->orderBy('created_at', 'asc') ->first(); }}
By default, the navigation uses the view
route. You can customize this:
<?php namespace App\Filament\Resources\PostResource\Pages; use App\Filament\Resources\PostResource;use Filament\Actions;use Filament\Resources\Pages\EditRecord; use Illuminate\Database\Eloquent\Model;use Nben\FilamentRecordNav\Concerns\WithRecordNavigation; class EditPost extends EditRecord{ use WithRecordNavigation; protected static string $resource = PostResource::class; protected function getRecordUrl(Model $record): string { return static::getResource()::getUrl('edit', ['record' => $record]); }}
You can customize the appearance of navigation buttons:
protected function getHeaderActions(): array{ return [ PreviousRecordAction::make() ->label('← Previous') ->color('secondary') ->size('sm'), NextRecordAction::make() ->label('Next →') ->color('secondary') ->size('sm'), ];}
The trait works seamlessly with edit pages:
<?php namespace App\Filament\Resources\PostResource\Pages; use App\Filament\Resources\PostResource;use Filament\Resources\Pages\EditRecord;use Nben\FilamentRecordNav\Actions\NextRecordAction;use Nben\FilamentRecordNav\Actions\PreviousRecordAction;use Nben\FilamentRecordNav\Concerns\WithRecordNavigation; class EditPost extends EditRecord{ use WithRecordNavigation; protected static string $resource = PostResource::class; protected function getHeaderActions(): array { return [ PreviousRecordAction::make(), NextRecordAction::make(), ]; }}
You can use the same navigation pattern across different resources:
// For Usersclass ViewUser extends ViewRecord{ use WithRecordNavigation; // ... configuration} // For Ordersclass ViewOrder extends ViewRecord{ use WithRecordNavigation; // ... configuration}
The package uses efficient database queries:
WithRecordNavigation
trait is used in your page classNextRecordAction
and PreviousRecordAction
are added to your getHeaderActions()
methodIf using a custom order column, make sure:
For large datasets:
where
clausesContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This package is open-sourced software licensed under the MIT license.
Made with ❤️ for the Filament PHP community