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 …
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,
Can someone help?
thanks in advance
BR
6 Replies
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
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()
@nighty did you find a solution? I'm also trying to add dynamic columns to a CSV export
@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...
@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
I, too, had gone the full custom route, but refactoring to use the native Filament Export worked great in my case!