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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay