FilamentF
Filamentβ€’2y ago
Arnaud

Add dynamic columns in table

Hello,

Not help but maybe somebody can find it when searching.

I was looking to add some columns to my table. I have a resource who has many states. The states can be created dynamically from the database and in my case the relation is MorphToMany πŸ˜…

But easy to add it to Filament :

1/ Add the getTableColumns() function
2/ Inside, push new columns to exist ones
$columns = [
  // others columns I've already created
];

foreach ($this->stages as $stage) {
  $columns[] = TextColumn::make('stages.['.$stage->id.'].name')
    ->label($stage->name)
    ->placeholder('-')
    ->toggleable(isToggledHiddenByDefault: true)
    ->state(function ($record) use ($stage) {
        $selectedStage = $record->stages->where('id', $stage->id)->first();

        if (! $selectedStage) { return; }
        return $selectedStage->pivot->created_at->format('d/m/Y');
  });
}

return $columns;

Enjoy
Was this page helpful?