How to access to the selected option in the SelectFilter?

I have provided options to a SelectFilter and everything works as expected. I have a special case where I need to modify the query if one specific option gets selected. How could I access to the selected option? Is it possible to modify the query based on the selected options?

SelectFilter::make('receiver')
  ->options(self::messageReceiver())
  ->searchable()
  ->attribute('chat_id')


I thought about chaining when method to the filter, but I just don't know how can I have access to the selected option so that I can replace the true part with the condition.

->when(true, function (BaseFilter $filter) {
  return $filter->modifyQueryUsing(function (Builder $query) {
    return $query->whereColumn('chat_id', 'sender_id');
  });
})
Solution
use ->query()

->query(function (Builder $query, array $data): Builder {
        return $query
            ->when(
                $data['created_until'],
                fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
            );
    })
Was this page helpful?