How can I create a checkbox filter that includes more records when active

Hey all I want my user to be able to enable inactive records using a filter. I currently do it like this:
TernaryFilter::make('show_inactive')
->label(__('Show inactive'))
->trueLabel('Yes')
->falseLabel('No')
->queries(
true: fn (Builder $query) => $query,
false: fn (Builder $query) => $query->whereNotIn('status', [
Status::Inactive,
]),
blank: fn (Builder $query) => $query->whereNotIn('status', [
Status::Inactive,
]),
),
TernaryFilter::make('show_inactive')
->label(__('Show inactive'))
->trueLabel('Yes')
->falseLabel('No')
->queries(
true: fn (Builder $query) => $query,
false: fn (Builder $query) => $query->whereNotIn('status', [
Status::Inactive,
]),
blank: fn (Builder $query) => $query->whereNotIn('status', [
Status::Inactive,
]),
),
The problem is that i want it to be a checkbox, when unchecked the inactive records should not be shown, when checked they should be shown. If you know a way I can have the same functionality but instead use a checkbox, please let me know.
3 Replies
Tjiel
TjielOP4w ago
Thanks for the help, I've implemented it the following way
Tables\Filters\Filter::make('show_inactive')
->form([
Checkbox::make('show_inactive'),
])->query(function (Builder $query, array $data) {
if (!$data['show_inactive']) {
$query->whereNotIn('status', [Status::Inactive]);
}
return $query;
})
Tables\Filters\Filter::make('show_inactive')
->form([
Checkbox::make('show_inactive'),
])->query(function (Builder $query, array $data) {
if (!$data['show_inactive']) {
$query->whereNotIn('status', [Status::Inactive]);
}
return $query;
})
LeandroFerreira
exactly.. any issues?
Want results from more Discord servers?
Add your server