Global search

搜尋條件

以什麼欄位搜尋

// 單獨
protected static ?string $recordTitleAttribute = 'title';

// 多個
public static function getGloballySearchableAttributes(): array
{
    return ['title', 'slug', 'author.name', 'category.name'];
}

客製化搜尋結果欄位顯示,舉例來說 title = Yish 前面想要添加 author.name:

public static function getGlobalSearchResultTitle(Model $record): string
{
    return $record->author->name .':'. $record->name;
}

添加更多內容在搜尋結果欄位內:

public static function getGlobalSearchResultDetails(Model $record): array
{
    return [
        'Author' => $record->author->name,
        'Category' => $record->category->name,
    ];
}

使用何種 query 來作搜尋,這邊可以看到我們必須先 eager load author 和 category:

public static function getGlobalSearchEloquentQuery(): Builder
{
    return parent::getGlobalSearchEloquentQuery()->with(['author', 'category']);
}

搜尋結果欄位內出現按鈕選項:

    public static function getGlobalSearchResultActions(Model $record): array
    {
        return [

            Action::make('edit')
                ->url(static::getUrl('edit', ['record' => $record]), shouldOpenInNewTab: true), //shouldOpenInNewTab=是否開新分頁
        ];
    }

限制搜尋結果數量

protected static int $globalSearchResultsLimit = 20;

關閉全局搜尋 AdminPanelProvider or customize panel provider,自訂鍵盤快捷鍵:

$panel->globalSearch(false)
->globalSearchKeyBindings(['command+k', 'ctrl+k']);