F
Filament9mo ago
Ray.nl

export modifyQuery with options

I have a custom page using ExportAction, now I'd like the user to have the possibility to select a custom date range. Now I'm wondering two thing: - How can I fill the options with a default value - How can I access the options in the modifyQuery function ? Thanks!
9 Replies
KeyMe
KeyMe9mo ago
following this
LJ
LJ9mo ago
$action->getOptions() does the trick for me in modifyQueryString
KeyMe
KeyMe9mo ago
modifyquerystring? one possible way tht i know is to define both option form and modifyquery on the export action itself, so we can call the array $data arg
LJ
LJ9mo ago
Yes https://filamentphp.com/docs/3.x/actions/prebuilt-actions/export#modifying-the-export-query
ExportAction::make()
->exporter(ProductExporter::class)
->modifyQueryUsing(fn (Builder $query) => $query->where('is_active', true))
ExportAction::make()
->exporter(ProductExporter::class)
->modifyQueryUsing(fn (Builder $query) => $query->where('is_active', true))
LJ
LJ9mo ago
in there you can also pass Action $action and do $action->getOptions() Ah, I see I mistyped originally - it's not modifyQueryString but modifyQueryUsing
kool
kool9mo ago
ExportAction::make()
->exporter(OrderExporter::class)
->form([
Fieldset::make('ordered_at') // ordered_at is a column in orders table
->label('Ordered At within Date Range')
->schema([
DateTimePicker::make('from')
->placeholder('Start Date')
->timezone(config('app.timezone_display')), // optional
DateTimePicker::make('to')
->placeholder(__('End Date'))
->timezone(config('app.timezone_display')), // optional
])->columns(1),
])
->modifyQueryUsing(
fn(OrderBuilder $query, array $data) => $query
->whereOrderedAtWithin($data['from'], $data['to'])
);
ExportAction::make()
->exporter(OrderExporter::class)
->form([
Fieldset::make('ordered_at') // ordered_at is a column in orders table
->label('Ordered At within Date Range')
->schema([
DateTimePicker::make('from')
->placeholder('Start Date')
->timezone(config('app.timezone_display')), // optional
DateTimePicker::make('to')
->placeholder(__('End Date'))
->timezone(config('app.timezone_display')), // optional
])->columns(1),
])
->modifyQueryUsing(
fn(OrderBuilder $query, array $data) => $query
->whereOrderedAtWithin($data['from'], $data['to'])
);
// OrderBuilder.php
public function whereOrderedAtWithin(?string $from, ?string $to): self
{
return $this
->when($from, fn ($query) => $query->where('ordered_at', '>=', $from))
->when($to, fn ($query) => $query->where('ordered_at', '<=', $to));
}
// OrderBuilder.php
public function whereOrderedAtWithin(?string $from, ?string $to): self
{
return $this
->when($from, fn ($query) => $query->where('ordered_at', '>=', $from))
->when($to, fn ($query) => $query->where('ordered_at', '<=', $to));
}
kool
kool9mo ago
No description
kool
kool9mo ago
@Ray.nl check this out if it helps
Ray.nl
Ray.nlOP9mo ago
Thank you, I will try!

Did you find this page helpful?