© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
5 replies
Ralph Morris

Modify query in Resource if not searching

Hey all, I am trying to only include top level pages on a PageResource by modifying the eloquent query on the PageResource. You can then access child pages via a PageRelationshipManager. However if a user searches, I want to include the child pages in the PageResource search result. My code is below:

public static function getEloquentQuery(): Builder
{
    return parent::getEloquentQuery()->when(!request('tableSearch'), function($query) {
        $query->whereNull('parent_id');
    });
}
public static function getEloquentQuery(): Builder
{
    return parent::getEloquentQuery()->when(!request('tableSearch'), function($query) {
        $query->whereNull('parent_id');
    });
}


This works if you refresh the page as request('tableSearch') returns the seaerch query. But if you just search and let livewire do the ajax search, request('tableSearch') returns null.

Any ideas? Thanks
Solution
I was able to do it by implementing the table method on the ListPages class.
    public function table(Table $table): Table
    {
        return static::getResource()::table($table)
            ->modifyQueryUsing(function(Builder $query) {
                return $query->when(empty($this->tableSearch), function($query) {
                    $query->whereNull('parent_id');
                });
            });
    }
    public function table(Table $table): Table
    {
        return static::getResource()::table($table)
            ->modifyQueryUsing(function(Builder $query) {
                return $query->when(empty($this->tableSearch), function($query) {
                    $query->whereNull('parent_id');
                });
            });
    }
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Modify Query Used in A Resource Page
FilamentFFilament / ❓┊help
3y ago
modify resource breadcrumbs
FilamentFFilament / ❓┊help
3y ago
Modify form or table in extended resource
FilamentFFilament / ❓┊help
3y ago
Modify the search value prior to searching?
FilamentFFilament / ❓┊help
3y ago