PHP code example of wezlo / filament-search-spotlight

1. Go to this page and download the library: Download wezlo/filament-search-spotlight library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wezlo / filament-search-spotlight example snippets


use Wezlo\FilamentSearchSpotlight\FilamentSearchSpotlightPlugin;

->plugins([
    FilamentSearchSpotlightPlugin::make(),
])

FilamentSearchSpotlightPlugin::make()
    // Keyboard binding (Mousetrap syntax). Accepts a string or an array.
    ->keyBinding('mod+k')

    // Placeholder text for the search input.
    ->placeholder('Jump to…')

    // Any valid CSS width (rem, px, vw, %, …). Applied as an inline style so
    // it is not subject to Tailwind purging.
    ->maxWidth('36rem')

    // Max results per category.
    ->resultLimitPerCategory(8)

    // Toggle built-in categories (all default on).
    ->records()           // records(false) to hide
    ->resources()
    ->pages()
    ->actionsEnabled()

    // Or completely override the category list with your own.
    ->categories([MyCustomCategory::class])

    // Exclude resources from both the Records and Resources categories
    // (also prevents their auto-generated Create action).
    ->excludeResources([AuditLogResource::class])

    // Register actions scoped to this panel (on top of the global registry).
    ->action(
        SpotlightAction::make('log-out')
            ->label('Log out')
            ->icon('heroicon-o-arrow-right-on-rectangle')
            ->keywords(['signout', 'quit'])
            ->group('Account')
            ->url(fn () => filament()->getLogoutUrl()),
    )
    ->actions([
        SpotlightAction::make('impersonate')->label('Impersonate user')->url('/impersonate'),
    ])

    // Hide actions registered in the global registry by name. Plugin-scoped
    // actions with the same name automatically override their global twin,
    // so overrideActions() is only needed when you want to hide without
    // replacing.
    ->overrideActions(['legacy-action'])

    // Skip the auto-generated "Create {Resource}" entries entirely.
    ->disableCreateActions()

    // Remove Filament's in-topbar global search input in favor of the overlay.
    ->disableDefaultGlobalSearch();

return [
    'key_binding' => 'mod+k',

    'result_limit_per_category' => 8,

    'excluded_resources' => [],

    'disable_default_global_search' => false,

    'categories' => [
        'records' => true,
        'resources' => true,
        'pages' => true,
        'actions' => true,
    ],

    'disable_create_actions' => false,

    'placeholder' => 'Search…',

    'max_width' => '42rem',

    'override_actions' => [],
];

use Wezlo\FilamentSearchSpotlight\Actions\SpotlightAction;

SpotlightAction::make('log-out')
    ->label('Log out')
    ->icon('heroicon-o-arrow-right-on-rectangle')
    ->keywords(['signout', 'quit'])
    ->group('Account')
    ->url(fn () => filament()->getLogoutUrl());

public function boot(): void
{
    SpotlightAction::make('clear-cache')
        ->label('Clear application cache')
        ->keywords(['flush', 'cache'])
        ->url(fn () => route('admin.cache.clear'))
        ->register();
}

FilamentSearchSpotlightPlugin::make()
    ->action(SpotlightAction::make('log-out')->label('Custom Log out')->url('/custom-logout'));

use Wezlo\FilamentSearchSpotlight\Categories\Category;
use Wezlo\FilamentSearchSpotlight\Data\SpotlightResult;

class SettingsCategory implements Category
{
    public function key(): string { return 'settings'; }

    public function label(): string { return 'Settings'; }

    public function search(string $query, int $limit): array
    {
        return collect($this->all())
            ->filter(fn ($item) => str_contains(strtolower($item['label']), strtolower($query)))
            ->take($limit)
            ->map(fn ($item) => new SpotlightResult(
                id: 'settings:'.$item['key'],
                category: 'settings',
                title: $item['label'],
                subtitle: null,
                icon: 'heroicon-o-cog-6-tooth',
                url: $item['url'],
            ))
            ->values()
            ->all();
    }

    protected function all(): array { /* … */ }
}

FilamentSearchSpotlightPlugin::make()
    ->categories([
        \Wezlo\FilamentSearchSpotlight\Categories\RecordsCategory::class,
        \Wezlo\FilamentSearchSpotlight\Categories\ResourcesCategory::class,
        SettingsCategory::class,
    ]);
bash
php artisan test --compact tests/Feature/FilamentSearchSpotlight