How to do a global search using Filament

Filament

The global search isn’t visible by default. To activate it, do this in the resource.

public static function getGloballySearchableAttributes(): array
{   
   return ['first_name', 'last_name', 'company_name', 'email'];
}
lang-php

Reference:

https://filamentphp.com/docs/3.x/panels/resources/global-search

Once you solve this problem, you’ll find the search results doesn’t show you expected nice friendly results. You have to first do this:

protected static ?string $recordTitleAttribute = 'name';
lang-php

Here is an actual implementation. It's from a stock market share lookup program. The name is the name of a symbol and the company name is what it is.

public static function getGlobalSearchResultTitle(Symbol|\Illuminate\Database\Eloquent\Model $record): string|\Illuminate\Contracts\Support\Htmlable
{
    return $record->company->name . ' - ' . $record->name;
}

public static function getGloballySearchableAttributes(): array
{
    return ['company.name', 'name'];
}
lang-php