Git Product home page Git Product logo

filament-scout's Introduction

Filament Scout Plugin

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Filament Scout Plugin

Plugin to integrate Laravel Scout into Filament Global Search and Table Search. Plus a ScoutSelect component which enhances the standard Select Field with scout search capabilities.

Pre-Requesites

  • Laravel Scout: Install and configure Laravel Scout as described in the Laravel Docs.

Installation

You can install the package via composer:

composer require kainiklas/filament-scout

Table Search

To use Scout Search instead of the default search on a table, add the trait InteractsWithScout to any Page which contains a table, e.g. app\Filament\Resources\MyResource\Pages\ListMyResources.php:

use Kainiklas\FilamentScout\Traits\InteractsWithScout;

class ListMyResources extends ListRecords
{
    use InteractsWithScout;
}

The table defined in the resource needs to be searchable() as described in the Filament table docs. Making each column searchable is not required anymore, as the content of what is searchable is defined within scout.

Global Search

  1. Check how to enable Global Search in the Filament Documentation.
class ContractResource extends Resource
{
    // required to enable global search
    protected static ?string $recordTitleAttribute = 'name';

    // optional: details
    public static function getGlobalSearchResultDetails(Model $record): array
    {
        return [
            'Category' => $record->category->name,
        ];
    }
}
  1. Add the Plugin FilamentScoutPlugin to your panel configuration, e.g., in app\Providers\Filament\AdminPanelProvider.php.
use Kainiklas\FilamentScout\FilamentScoutPlugin;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            $plugins([
                FilamentScoutPlugin::make()
            ]);
    }
}

Meilisearch

If you are using Meilisearch, you can activate meilisearch specific features (search context highlighting):

  1. Configure the plugin.
use Kainiklas\FilamentScout\FilamentScoutPlugin;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            $plugins([
                FilamentScoutPlugin::make()
                    ->useMeilisearch() // enables meilisearch specific features
            ]);
    }
}
  1. (Optional) Implement/ Adapt getGlobalSearchResultDetails() in your Resource:
public static function getGlobalSearchResultDetails(Model $record): array
    {
        // change the filament default implementation from this
        // return [
        //     'AttributeTitle' => $record->attribute_name
        // ];
        
        // to this
        return [
            'scout_attribute_name' => "AttributeTitle"
        ];
    }

Select Form Field

To enable scout search in your select form fields use the provided ScoutSelect component:

use Kainiklas\FilamentScout\Forms\Components\ScoutSelect;
 
ScoutSelect::make('company_id')
    ->searchable()
    ->relationship('company', 'name')
    ->useScout() // must be called after relationship()

Technically, the ScoutSelect component inherits from Filament\Forms\Components\Select. The useScout() method sets a new getSearchResultsUsing() closure which uses scout.

Important: The useScout() method needs to be called after the relationship method. Otherwise it is overriden by the relationship() method.

Hint: Only values which are accessible and defined by scout are searchable.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

filament-scout's People

Contributors

kainiklas avatar dependabot[bot] avatar github-actions[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.