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 …


This content originally appeared on DEV Community and was authored by Ariel Mejia

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()
    ])

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']);
}


This content originally appeared on DEV Community and was authored by Ariel Mejia


Print Share Comment Cite Upload Translate Updates
APA

Ariel Mejia | Sciencx (2024-06-30T18:32:44+00:00) Customize Filament Table Query. Retrieved from https://www.scien.cx/2024/06/30/customize-filament-table-query/

MLA
" » Customize Filament Table Query." Ariel Mejia | Sciencx - Sunday June 30, 2024, https://www.scien.cx/2024/06/30/customize-filament-table-query/
HARVARD
Ariel Mejia | Sciencx Sunday June 30, 2024 » Customize Filament Table Query., viewed ,<https://www.scien.cx/2024/06/30/customize-filament-table-query/>
VANCOUVER
Ariel Mejia | Sciencx - » Customize Filament Table Query. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/30/customize-filament-table-query/
CHICAGO
" » Customize Filament Table Query." Ariel Mejia | Sciencx - Accessed . https://www.scien.cx/2024/06/30/customize-filament-table-query/
IEEE
" » Customize Filament Table Query." Ariel Mejia | Sciencx [Online]. Available: https://www.scien.cx/2024/06/30/customize-filament-table-query/. [Accessed: ]
rf:citation
» Customize Filament Table Query | Ariel Mejia | Sciencx | https://www.scien.cx/2024/06/30/customize-filament-table-query/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.