DEV Community

Ariel Mejia
Ariel Mejia

Posted on

Customize Filament Table Query

I am going to share how you can customize the query applying filters, this tip has a meilisearch example borrowed as I found the tip in a laracast chat, the content was enriched by me.

To customize the table search to use some Laravel Scout Driver to get a fuzzyness search or a more powerful search in terms of speed:

The Inline Way

// filter criteria
$filter = '(status = Active) AND (type = big)';

// $this->query is a prop provided by extending from filament resource class
$company_ids = \App\Models\Company::search($this->query, function (\Meilisearch\Endpoints\Indexes $meilisearch, $query, $options) use($filter) {
    // This is a custom configuration for Meilisearch only
    $options['facets'] = ['type','status'];
    $options['filter'] = $filter;
    $options['hitsPerPage'] = 100;
    return $meilisearch->search($query, $options);
})->get()->pluck('id');

return $table
    ->query(\App\Models\Company::whereIn('id', $company_ids))
    ->columns([
        TextColumn::make('name')->sortable(),
        TextColumn::make('status')->sortable(),
        TextColumn::make('type')->sortable()
    ])
Enter fullscreen mode Exit fullscreen mode

The Override Way

We can also override the getEloquentQuery method, like this example removing a global scope for soft deletes:

public static function getEloquentQuery(): Builder
{
    return parent::getEloquentQuery()
        ->withoutGlobalScopes([SoftDeleted::class])
        ->with(['products']);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)