Apply active table filters with headerActions export
imbue opened this issue ยท 6 comments
Is it currently possible to apply the active table filters when exporting using the ExportAction as header action?
Currently this ExportAction will export all records of that db table. It would be great if the filters could be applied to the export query so the user can create an export for i.e. a specific date range:
->filters([
Tables\Filters\Filter::make('created_at')
->form([
Forms\Components\DatePicker::make('created_from')
->placeholder(fn($state): string => 'Dec 18, ' . now()->subYear()->format('Y')),
Forms\Components\DatePicker::make('created_until')
->placeholder(fn($state): string => now()->format('M d, Y')),
])
->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['created_from'] ?? null) {
$indicators['created_from'] = 'Order from ' . Carbon::parse($data['created_from'])->toFormattedDateString();
}
if ($data['created_until'] ?? null) {
$indicators['created_until'] = 'Order until ' . Carbon::parse($data['created_until'])->toFormattedDateString();
}
return $indicators;
}),
])
->headerActions([
ExportAction::make()->exports([
ExcelExport::make()->withColumns([
Column::make('case_number')->heading('Case number'),
Column::make('created_at')->heading('Creation date'),
]),
])
])
I think this was implemented when you use ->fromTable()
A little bit off-issue-topic but is there a way to export related data on a row-for-every-relation basis? I.e some orders have 1 order line, some have 2 or more.
Order | Product | Quantity |
---|---|---|
ORD001 | product_a | 1 |
ORD001 | product_b | 3 |
ORD002 | product_c | 1 |
I've considered a couple of options but each of them has a blocker:
- #47, but this merges them into a single excel row
- #8, but it looks like this will lose the functionality to use the active table filters
- the
Column::make('ordersLines[0].product')
syntax, but this lacks flexibility due to being limited to that index
Any ideas?
Not really. Haven't found a good way to implement relation across multiple columns yet.
Solution:
ExportAction::make()
->exports([
ExcelExport::make()
->fromModel()
->useTableQuery(),
]),
This uses the model attributes and the currently active table query.