F
Filament5mo ago
jjo63

ToggleColumn - making conditionally invisible

Hi I have the following code snippet
ToggleColumn::make('is_indexed')
->label('Index?')
->onColor('info')
->beforeStateUpdated(function ($record, $state) {

// Runs before the state is saved to the database.
})
->afterStateUpdated(function ($record, $state) {
// Runs after the state is saved to the database.
})
->visible(function ($record) {
dd($record->record_type);

})
ToggleColumn::make('is_indexed')
->label('Index?')
->onColor('info')
->beforeStateUpdated(function ($record, $state) {

// Runs before the state is saved to the database.
})
->afterStateUpdated(function ($record, $state) {
// Runs after the state is saved to the database.
})
->visible(function ($record) {
dd($record->record_type);

})
In the
beforeStateUpdated
beforeStateUpdated
I can access $record - but within
visible
visible
I am unable to (i.e. generates an error - "Attempt to read property "record_type" on null"). I don't understand why $record isn't universally available to me - but accepting that's the case, how would I access the current record within the
visible
visible
method so that I can determine whether the toggle should be shown or not? thanks for your help! j
5 Replies
LeandroFerreira
LeandroFerreira5mo ago
You can’t determine the visibility of a column by a row try disabled instead
jjo63
jjo63OP5mo ago
I guess that would explain why $record isn't available as that must only apply to rows - what I was hoping to achieve was that if a column on the underlying db table had value 'X' then the toggle will be shown; else it will be hidden. Is that achievable (perhaps through another method)? Oh I see your reference to "disabled" as an alternative - I was hoping to hide it from view altogether (i.e. blank cell) on rows where it shouldn't be displayed.
Dennis Koch
Dennis Koch5mo ago
Maybe you can get this to work via ->extraAttributes(fn ($record) => ['class' => 'class-that-targets-the-toggle']). Not sure whether that works with $record but worth a try
jjo63
jjo63OP5mo ago
Thanks for the tip Dennis. I wonder if it's a useful feature to support natively (i.e. ability to programatically show/hide a toggle on a row-by-row basis) - is there a process by which I would propose this as an enhancement?
Dennis Koch
Dennis Koch5mo ago
We have GitHub discussions for that. In general Editable Columns are quite basic and we don't see too many requests related to them
Want results from more Discord servers?
Add your server