FilamentF
Filament8mo ago
nighty

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('...'),
];


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
        });
];


//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
Was this page helpful?