PHP code example of dvictor357 / laravel-omnisearch

1. Go to this page and download the library: Download dvictor357/laravel-omnisearch 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/ */

    

dvictor357 / laravel-omnisearch example snippets


'shortcuts' => ['k', '/'],      // Multiple shortcuts supported
'modifier' => 'cmd',             // 'cmd', 'ctrl', or 'alt'

'models' => [
    App\Models\User::class => [
        'columns' => ['name', 'email'],    // Columns to search
        'title' => 'name',                 // Display title
        'description' => 'email',          // Display subtitle
        'route' => 'users.show',           // Named route (receives model ID)
        'icon' => 'user',                   // Icon identifier
        'limit' => 5,                      // Max results for this model
        'group' => 'Users',                // Custom group label (optional)
    ],
],

'routes' => [
    ''api.*', 'sanctum.*', 'livewire.*'],
],

'ui' => [
    'placeholder' => 'Search anything...',
    'debounce' => 300,
    'max_results' => 10,
    'show_keyboard_hints' => true,
    'max_recent_searches' => 10,
    'enable_history' => true,
    'theme' => [
        'primary' => '#8b5cf6',
        'bg' => 'rgba(30, 30, 46, 0.85)',
        'radius' => '16px',
        'accent' => 'rgba(139, 92, 246, 0.3)',
    ],
],

'search' => [
    'use_scoring' => true,          // Enable relevance scoring
    'min_score' => 0,
    'highlight_matches' => true,
],

use OmniSearch\Contracts\SearchSource;
use OmniSearch\Data\Result;
use Illuminate\Support\Collection;

class MyCustomSource implements SearchSource
{
    public function getKey(): string
    {
        return 'custom';
    }

    public function getLabel(): string
    {
        return 'My Results';
    }

    public function getIcon(): string
    {
        return 'star';
    }

    public function authorize(): bool
    {
        return auth()->check();
    }

    public function getSynonyms(): array
    {
        return ['my-result', 'custom-result'];
    }

    public function getDependencies(): array
    {
        return [];
    }

    public function search(string $query): Collection
    {
        return collect([
            Result::navigate(
                id: 'custom:1',
                title: 'Custom Item',
                description: 'A custom search result',
                url: '/custom/1',
                icon: 'star',
                group: $this->getLabel(),
            ),
        ]);
    }
}

use OmniSearch\Sources\CommandSource;
use OmniSearch\Data\Result;
use Illuminate\Support\Collection;

class CreateUserCommand extends CommandSource
{
    public function getKey(): string
    {
        return 'create-user';
    }

    public function getLabel(): string
    {
        return 'Create User';
    }

    public function getIcon(): string
    {
        return 'user-plus';
    }

    public function getDependencies(): array
    {
        return [
            'team' => App\Search\TeamSearchDependency::class,
        ];
    }

    public function search(string $query): Collection
    {
        return collect([
            Result::modal(
                id: 'command:create-user',
                title: 'Create New User',
                description: 'Open user creation form',
                modalName: 'create-user-modal',
                icon: 'user-plus',
                group: 'Commands',
            ),
        ]);
    }

    public function execute(...$args): mixed
    {
        // Command execution logic
    }
}

Result::copy(
    id: 'copy:email',
    title: 'Copy Email',
    description: 'Click to copy user email',
    textToCopy: $user->email,
    icon: 'copy',
    group: 'Actions',
);

'sources' => [
    OmniSearch\Sources\ModelSource::class,
    OmniSearch\Sources\RouteSource::class,
    App\Search\MyCustomSource::class, // Add your source
],

use OmniSearch\Events\SearchPerformed;
use OmniSearch\Events\ResultSelected;
use OmniSearch\Events\ModalOpened;

// Listen for search events
Event::listen(SearchPerformed::class, function ($event) {
    // $event->query, $event->resultsCount, $event->duration
});

// Listen for result selection
Event::listen(ResultSelected::class, function ($event) {
    // $event->id, $event->title, $event->actionType, $event->url
});

// Listen for modal open
Event::listen(ModalOpened::class, function ($event) {
    // $event->trigger
});
bash
php artisan omnisearch:install
bash
php artisan vendor:publish --tag=omnisearch-config
php artisan vendor:publish --tag=omnisearch-views
bash
# Install OmniSearch assets
php artisan omnisearch:install

# Create a new search source
php artisan omnisearch:make-source MyCustom

# Clear search cache
php artisan omnisearch:clear-cache