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


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');
                });
            });
    }
Was this page helpful?