Exporter - dynamic generated columns

Hello All, i have an Exporter Class that extends "Exporter": Example BookExporter: - We would like to Export Books - a Book can have Multiple Authors lets say: Normlally we just Define Columns in the Exporter …
return [
ExportColumn::make('BookId'),
ExportColumn::make('...'),
ExportColumn::make('...'),
];
return [
ExportColumn::make('BookId'),
ExportColumn::make('...'),
ExportColumn::make('...'),
];
Now, my Problem is, when exporting the Authors - i would like to have for each Author an Extra Column, no matter how many Authors are present. For Example: Book 1 -> Author1, Author2, Author3 Book 2 -> Author2, Author4, Author5, Author7 Book 2 -> Author5, Author8, Author15, Author23, …. i would like to create an Output like this (Column Names): BookId, Book..., Book..., Author1, Author2, Author3, AuthorN,... i think it´s totally possible to achieve this inside the getColumns() Method. when creating columns with an foreach loop, i need to get the current record without beeing in an Exportcolumn,
//how it is normally done
return [
ExportColumn::make('Column')
->formatStateUsing(function(?Model $record){
//do work here
});
];
//how it is normally done
return [
ExportColumn::make('Column')
->formatStateUsing(function(?Model $record){
//do work here
});
];
//What i need is something like this:

$record = null //do not know how to access here

$authorColumns = [];
$i = 1;
foreach($record->authors as $author){
$authorColumns[] = ExportColumn::make('Author'.$i)
->formatStateUsing(function($author){
//do work here
});
$i++;
}

return array_merge($columns, $authorColumns);
//What i need is something like this:

$record = null //do not know how to access here

$authorColumns = [];
$i = 1;
foreach($record->authors as $author){
$authorColumns[] = ExportColumn::make('Author'.$i)
->formatStateUsing(function($author){
//do work here
});
$i++;
}

return array_merge($columns, $authorColumns);
Can someone help? thanks in advance BR
6 Replies
nighty
nightyOP4w ago
i think the builtin export function is not made for such complex cases - i endet up writing a custom exporter for this - someone has maybe some hints though, would be appreciated. thx
Antea
Antea4w ago
Maybe get the max amount of authors of all books and add that many columns. Something like ->columns(fn() =>[ //other columns, ...collect(range(0, Book::withCount('authors')->max('authorscount'))->map(fn($key) => Column::make(author$key)) ->toArray()
stursby
stursby3w ago
@nighty did you find a solution? I'm also trying to add dynamic columns to a CSV export
stursby
stursby3w ago
@nighty I ended up finding this and it was helpul! https://www.answeroverflow.com/m/1359449676558110830
Dynamic ExportColumns based on JSON field in Exporter, possible? - ...
Hi, is it possible to define extra dynamic export columns based on a JSON array from a owner record in a relation manager? My use case: I've got a Course resource with a CourseRegistration (HasMany) relation manager. The Course resource contains a Builder field that is used to insert extra form fields for when people sign up for a course. For ex...
nighty
nightyOP3w ago
@stursby i did an entire exporter myself to have the full flexibility here, just generated the CSV from Scratch in this case ... but what you found is very interesting thx
stursby
stursby2w ago
I, too, had gone the full custom route, but refactoring to use the native Filament Export worked great in my case!

Did you find this page helpful?