In addition to searching in the table or the column, Filament offers a so-called "Global search" in multiple Models. It's very simple to activate so this lesson will be pretty short.
To enable that search, you don't have to configure it at all. Instead, you just specify searchable properties in each Resource you need.
For example, you want Filament to search for Products by their names. You just add a property $recordTitleAttribute
into the Resource:
app/Filament/Resources/ProductResource.php:
class ProductResource extends Resource{ protected static ?string $recordTitleAttribute = 'name';
And that's it: on the top-right, you will see a search showing the products by name.
If you click on the search result, you will go to that resource's Edit page. If you want to change that to a View page, for example, you need to specify this method:
app/Filament/Resources/ProductResource.php:
use Illuminate\Database\Eloquent\Model; class ProductResource extends Resource{ public static function getGlobalSearchResultUrl(Model $record): string { return self::getUrl('view', ['record' => $record]); }
Notice: global search will not work on "simple" Resources because they don't have Edit/View pages, so there's nowhere to redirect.
For example, our Category/Tag Resources are simple. Their create/edit forms are only in modals, so even if we specify the $recordTitleAttribute
for Categories, they wouldn't appear in the search results.
If you want to search by multiple fields in the same DB table, specify them as an array in a method getGloballySearchableAttributes()
of the Resource.
Let's say your "products" table has fields like "name" and "description":
Then you specify them like this:
app/Filament/Resources/ProductResource.php:
class ProductResource extends Resource{ // ... public static function getGloballySearchableAttributes(): array { return ['name', 'description']; }
And then, Filament would find a product by description, too, still showing the $recordTitleAttribute
field value in the search results.
By default, Filament will show up to 50 search results. In our case, there are 5 products with similar names:
But you may customize it to show only the top 3, for example. Just define this property in the Resource:
app/Filament/Resources/ProductResource.php:
class ProductResource extends Resource{ protected static int $globalSearchResultsLimit = 3;
After this change, you see only 3 results:
The last thing in this lesson: it's pretty common to have a keyboard shortcut for global search.
You can configure it in the main PanelProvider, with the method called globalSearchKeyBindings()
:
app/Providers/Filament/AdminPanelProvider.php:
class AdminPanelProvider extends PanelProvider{ public function panel(Panel $panel): Panel { return $panel ->default() // ... other configurations ->globalSearchKeyBindings(['command+k', 'ctrl+k']); }}
Now, whenever I click one of those shortcuts while browsing the website, Filament will put the focus and the cursor into the search input, so I can immediately start typing the search query.
And that's it about global search, in a nutshell. As I mentioned, this lesson was quite short, as it's that easy to set up.