DEV Community

Ariel Mejia
Ariel Mejia

Posted on

15

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs