F
Filament8mo 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
KeyMe8mo ago
following this
Lukas Jankauskas
$action->getOptions() does the trick for me in modifyQueryString
KeyMe
KeyMe8mo 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
Lukas Jankauskas
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))
Lukas Jankauskas
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
kool7mo 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
kool7mo ago
No description
kool
kool7mo ago
@Ray.nl check this out if it helps
Ray.nl
Ray.nlOP7mo ago
Thank you, I will try!
Want results from more Discord servers?
Add your server