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?
2 Replies
Dennis Koch
Dennis Koch2w ago
EloquentQuery is the wrong place. But you can use them with $table->modifyQuery(fn ($livewire) => $livewire-> tableFilters
theageddeveloper
thx for tip Dennis
Want results from more Discord servers?
Add your server