© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
2 replies
theageddeveloper

How to use table filters in getEloquentQuery?

Hi,
is there possibility to use filters inside getEloquentQuery? I have custom case with big query inside getEloquentQuery method, that has sub queries inside and my select filter would have to add where conditions to my subqueries.
I tried to do something like this:
public static function table(Table $table): Table
    {
             ->filters([
                SelectFilter::make('game')
                    ->label('Game')
                    ->options(GameEnum::class)
                     ->query(function ($query) {
                     })
            ])
 }
 
  #[Override]
    public static function getEloquentQuery(): Builder
    {
        $tableFilters = request()->query('tableFilters');
        $game = isset($tableFilters['game']['value']) ? $tableFilters['game']['value'] : null;

        return parent::getEloquentQuery()
            ->select('users.*')
            ->leftJoinSub(
                \DB::table('othertable')
                    ->select('custom_column_id', \DB::raw('COUNT(*) as count'))
                    ->when($game, function ($query, $game) {
                        $query->where('enquiry->game', $game);
                    })
                    ->groupBy('custom_column_id'),
                'counts',
                function ($join) {
                    $join->on('users.id', '=', 'counts.match_id');
                }
            )
            .....
public static function table(Table $table): Table
    {
             ->filters([
                SelectFilter::make('game')
                    ->label('Game')
                    ->options(GameEnum::class)
                     ->query(function ($query) {
                     })
            ])
 }
 
  #[Override]
    public static function getEloquentQuery(): Builder
    {
        $tableFilters = request()->query('tableFilters');
        $game = isset($tableFilters['game']['value']) ? $tableFilters['game']['value'] : null;

        return parent::getEloquentQuery()
            ->select('users.*')
            ->leftJoinSub(
                \DB::table('othertable')
                    ->select('custom_column_id', \DB::raw('COUNT(*) as count'))
                    ->when($game, function ($query, $game) {
                        $query->where('enquiry->game', $game);
                    })
                    ->groupBy('custom_column_id'),
                'counts',
                function ($join) {
                    $join->on('users.id', '=', 'counts.match_id');
                }
            )
            .....

In this case $game parameter is set on initial load but later on is null despite being present in the url. Probably it is being cleared.
How to handle such complex cases?
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

table filters are not working when i customized getEloquentQuery()
FilamentFFilament / ❓┊help
17mo ago
How to use deep relations in filters in the V3 Table Builder?
FilamentFFilament / ❓┊help
3y ago
Having in Table Filters
FilamentFFilament / ❓┊help
3y ago
How to get the filters currently in use on a rendered table.
FilamentFFilament / ❓┊help
3y ago