Conditionally hide SelectColumn based on email domain

I'm working on a Laravel project using FilamentPHP and need some help conditionally hiding a column in a table.
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('email')
->searchable(),
Tables\Columns\SelectColumn::make('department_id')
->label('Department')
->options(Department::all()->pluck('name', 'id')),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('email')
->searchable(),
Tables\Columns\SelectColumn::make('department_id')
->label('Department')
->options(Department::all()->pluck('name', 'id')),
]);
}
The department_id column should only display when the user’s email address does end with @mycompany.com. If it doesn't, I'd like this column to be hidden for that specific row. Is there a way to conditionally hide a column based on a record's attribute like this in FilamentPHP?
6 Replies
LeandroFerreira
You can disable the select according to the condition
->disabled(fn (YourModel $record) => !str_ends_with($record->email, '@mycompany.com'))
->disabled(fn (YourModel $record) => !str_ends_with($record->email, '@mycompany.com'))
Guido
GuidoOP7d ago
Hi @LeandroFerreira Thanks for the suggestion! However, I’m actually looking to completely hide the column rather than just disable it. Appreciate the help!
LeandroFerreira
visible won't work because you can't determine the column visibility by rows..
devpoolxx
devpoolxx6d ago
GitHub
Visible Closure Table Column - Parameter Not Filled · filamentphp f...
Package Table builder Package Version v3.x.x How can we help you? I want to create a condition where columns can appear or hide according to the data row they pass. Usually we just enter a callback...
Guido
GuidoOP6d ago
@LeandroFerreira I see, thanks! Any ideas on a workaround to just leave the column empty for certain rows?
LeandroFerreira
maybe ->extraAttributes(fn (YourModel $record) => ['class' => str_ends_with($record->email, '@mycompany.com') ? '' : 'hidden']) use lifecycle hooks to verify the values...
Want results from more Discord servers?
Add your server