Create default filter to be today

I have a table and I want to make the default filter to be today so that today's record are showned initially
6 Replies
awcodes
awcodes9mo ago
That shouldn’t be a filter. You should modify the default query in that case. That way any other filters can be applied on top of it.
codeartisan
codeartisanOP9mo ago
Thank you fixed
ua
ua9mo ago
@codeartisan
H4L1M
H4L1M9mo ago
what if the user wants to see records from older dates
codeartisan
codeartisanOP9mo ago
like a range filter
Filter::make('date')
->form([
DatePicker::make('created_from')
->label('From'),
DatePicker::make('created_until')
->label('To'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('date', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('date', '<=', $date),
);
})
->indicateUsing(function (array $data): array {
$indicators = [];

if ($data['from'] ?? null) {
$indicators[] = Indicator::make('Created from ' . Carbon::parse($data['from'])->toFormattedDateString())
->removeField('from');
}

if ($data['until'] ?? null) {
$indicators[] = Indicator::make('Created until ' . Carbon::parse($data['until'])->toFormattedDateString())
->removeField('until');
}

return $indicators;
}),
Filter::make('date')
->form([
DatePicker::make('created_from')
->label('From'),
DatePicker::make('created_until')
->label('To'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('date', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('date', '<=', $date),
);
})
->indicateUsing(function (array $data): array {
$indicators = [];

if ($data['from'] ?? null) {
$indicators[] = Indicator::make('Created from ' . Carbon::parse($data['from'])->toFormattedDateString())
->removeField('from');
}

if ($data['until'] ?? null) {
$indicators[] = Indicator::make('Created until ' . Carbon::parse($data['until'])->toFormattedDateString())
->removeField('until');
}

return $indicators;
}),
I always that for a range filter or incase your using created_at here is the range filter as well
Filter::make('created_at')
->form([
DatePicker::make('created_from')
->label('From'),
DatePicker::make('created_until')
->label('To'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
);
})
->indicateUsing(function (array $data): array {
$indicators = [];

if ($data['from'] ?? null) {
$indicators[] = Indicator::make('Created from ' . Carbon::parse($data['from'])->toFormattedDateString())
->removeField('from');
}

if ($data['until'] ?? null) {
$indicators[] = Indicator::make('Created until ' . Carbon::parse($data['until'])->toFormattedDateString())
->removeField('until');
}

return $indicators;
}),
Filter::make('created_at')
->form([
DatePicker::make('created_from')
->label('From'),
DatePicker::make('created_until')
->label('To'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
)
->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
);
})
->indicateUsing(function (array $data): array {
$indicators = [];

if ($data['from'] ?? null) {
$indicators[] = Indicator::make('Created from ' . Carbon::parse($data['from'])->toFormattedDateString())
->removeField('from');
}

if ($data['until'] ?? null) {
$indicators[] = Indicator::make('Created until ' . Carbon::parse($data['until'])->toFormattedDateString())
->removeField('until');
}

return $indicators;
}),
awcodes
awcodes9mo ago
Then make a filter that allows for older dates. 🙂
Want results from more Discord servers?
Add your server