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. ```php public function table(Table $table): Table { return static::getResource()::table($table)...
Jump to solution
3 Replies
Dennis Koch
Dennis Koch11mo ago
request('tableSearch') returns null
You should access stuff via the Livewire component and not via request() when working with Livewire
Ralph Morris
Ralph MorrisOP11mo ago
Thanks. I'm not sure how I can access the livewire component though, as I'm inside a static method and nothing is passed to it?
Solution
Ralph Morris
Ralph Morris11mo ago
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');
});
});
}

Did you find this page helpful?