F
Filament7mo ago
Anish

DateConstraint

I am trying to build a filter using QueryBuilder. I am trying to use a DateConstraint in which I want to filter by Date time.
DateConstraint::make('application_submitted_at')->nullable(),
DateConstraint::make('application_submitted_at')->nullable(),
It is cast as datetime in the model. But the option for setting the filter value, provides only the date option, no time option is available. Further it seems that the form uses the native datepicker. Is there a way to set both date & time and the javascript based datepicker. Thanks in advance for any help.
3 Replies
Butterfly
Butterfly7mo ago
I believe that you are trying to achieve something like this:
Filter::make('application_submitted_at')
->form([
DatePicker::make('from'),
DatePicker::make('until'),
])->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['from'],
fn (Builder $query, $date): Builder => $query->whereDate('application_submitted_at', '>=', $date),
)
->when(
$data['until'],
fn (Builder $query, $date): Builder => $query->whereDate('application_submitted_at', '<=', $date),
);
});
Filter::make('application_submitted_at')
->form([
DatePicker::make('from'),
DatePicker::make('until'),
])->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['from'],
fn (Builder $query, $date): Builder => $query->whereDate('application_submitted_at', '>=', $date),
)
->when(
$data['until'],
fn (Builder $query, $date): Builder => $query->whereDate('application_submitted_at', '<=', $date),
);
});
Butterfly
Butterfly7mo ago
The example follows the one provided in the docs: https://filamentphp.com/docs/3.x/tables/filters/custom In order to use the JS-based date picker I believe you can add ->native(false) to the DatePickers
Anish
Anish7mo ago
Thanks. But that's not using the DateConstraint. This is creating each filter separately. Then we can only use the "and" operations. I find the query builder is much more flexible. Of course, if I am unable to find a solution I will probably go back to a similar solution.
Want results from more Discord servers?
Add your server