Skip to main content

Global Search in Multiple Resources

Premium
2:47

Text Version of the Lesson

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...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

A
alin ✓ Link copied!

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.

This was not my experience, on Filament v3, so i guess the same as you. I added:

protected static ?string $recordTitleAttribute = 'name';

To the TagResource and CategoryResource and they appear in the global search.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.