PHP code example of manggala / laravel-spotlight

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

    

manggala / laravel-spotlight example snippets


use Manggala\Spotlight\Facades\Spotlight;
use Manggala\Spotlight\Commands\SpotlightCommand;

public function boot(): void
{
    Spotlight::register(
        SpotlightCommand::make('General Settings')
            ->description('Manage application branding, mail, and SMTP configurations')
            ->icon('cog')
            ->group('Navigation')
            ->keywords(['setting', 'config', 'smtp', 'mail', 'site name'])
            ->action(fn () => redirect()->route('settings.general'))
    );

    SpotlightCommand::make('System Cache Clear')
        ->description('Flush application cache, routes, and views')
        ->icon('trash')
        ->group('Actions')
        ->action(function () {
            Artisan::call('optimize:clear');
            return back()->with('success', 'System cache cleared successfully!');
        });
}

use Manggala\Spotlight\Facades\Spotlight;
use App\Models\User;

Spotlight::search(User::class)
    ->group('Users')
    ->icon('user')
    ->query(function ($query, string $term) {
        return $query->where('name', 'like', "%{$term}%")
                    ->orWhere('email', 'like', "%{$term}%")
                    ->limit(8);
    })
    ->title(fn (User $user) => $user->name)
    ->subtitle(fn (User $user) => $user->email)
    ->avatar(fn (User $user) => $user->avatar_url)
    ->action(fn (User $user) => to_route('users.show', $user));

// Protect with Laravel Gate / Policy ability
SpotlightCommand::make('Financial Reports')
    ->group('Reports')
    ->can('view-financial-reports')
    ->action(fn () => to_route('reports.financial'));

// Protect with Spatie Roles
SpotlightCommand::make('Admin User Management')
    ->group('Administration')
    ->roles(['admin', 'super-admin'])
    ->action(fn () => to_route('admin.users'));

// Custom Closure Condition
SpotlightCommand::make('Pro Feature Dashboard')
    ->when(fn ($user) => $user->isProSubscriber())
    ->action(fn () => to_route('pro.dashboard'));

return [

    /*
    |--------------------------------------------------------------------------
    | Keyboard Shortcut Settings
    |--------------------------------------------------------------------------
    */
    'keybinding' => [
        'shortcut' => 'k', // Triggers Cmd+K or Ctrl+K
        'close_on_escape' => true,
    ],

    /*
    |--------------------------------------------------------------------------
    | Async Search Performance & Throttling
    |--------------------------------------------------------------------------
    */
    'search' => [
        'debounce_ms' => 150,
        'rate_limit_per_minute' => 60,
        'max_results_per_group' => 8,
    ],

    /*
    |--------------------------------------------------------------------------
    | UI & Visual Customization
    |--------------------------------------------------------------------------
    */
    'ui' => [
        'dark_mode' => true,
        'backdrop_blur' => true,
        'placeholder' => 'Type a command or search...',
    ],

];
bash
php artisan spotlight:install
bash
# Publish configuration file
php artisan spotlight:publish --tag=config

# Publish React component views
php artisan spotlight:publish --tag=views