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')
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');
});
})
->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...
Jump to solution
2 Replies
Solution
Lara Zeus
Lara Zeus15mo ago
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),
);
})
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
);
})
fsamapoor
fsamapoorOP15mo ago
Many thanks!
Want results from more Discord servers?
Add your server