F
Filamentβ€’17mo ago
Atena.D

Get other column value, filament tables

Hi. How can I get another column value in ->default() method in filament tables?
22 Replies
toeknee
toekneeβ€’17mo ago
Is this on load from the record, or is this when a change occurs
Atena.D
Atena.Dβ€’17mo ago
I just want to use that as the default value of another column. how can I get that in ->default() method? should I use closure ?
Dennis Koch
Dennis Kochβ€’17mo ago
fn ($record) => $record->field
Atena.D
Atena.Dβ€’17mo ago
this didn't work for me
toeknee
toekneeβ€’17mo ago
you need a closure with Dennis approach, but remember default will only be used if the value is truly empty. If using fill, you will need to add it to the fill method too. Defaults are designed for new forms
Dennis Koch
Dennis Kochβ€’17mo ago
Do TableColumns even have defaults? πŸ€”
toeknee
toekneeβ€’17mo ago
I assumed it was forms doh
Dennis Koch
Dennis Kochβ€’17mo ago
Your value probably isn't null then
toeknee
toekneeβ€’17mo ago
Then you need formatStateUsing()
Dennis Koch
Dennis Kochβ€’17mo ago
If there isn't a method called default, I at least expected an error message and not a "didn't work"
Atena.D
Atena.Dβ€’17mo ago
It's now working but does this work for other methods? because I need to use $record in ->hidden() method also but to specify a condition.
toeknee
toekneeβ€’17mo ago
Depends on the method, your IDE should show you all available methods
Atena.D
Atena.Dβ€’17mo ago
for example when I have something like this: ->hidden(fn ($record) => $record->factor_type === 'buy') it thows this error: Attempt to read property "factor_type" on null. Although it works for ->default() method
toeknee
toekneeβ€’17mo ago
I assume that is on a new form opposed to an edit, you need to check first $record is not null.
Atena.D
Atena.Dβ€’17mo ago
I have 3 records in this table which all of them have factor_type
toeknee
toekneeβ€’17mo ago
IT is saying the record is null not the factor_type which means the record hasn't been loaded in Try: fn( MyModel $record) =>
Atena.D
Atena.Dβ€’17mo ago
It again says it's null Argument #1 ($record) must be of type App\Models\Factors, null given, called in
toeknee
toekneeβ€’17mo ago
Are you creating or editing a record?
Atena.D
Atena.Dβ€’17mo ago
Actually it is in table builder it's not related to forms
toeknee
toekneeβ€’17mo ago
Ugh my bad
Dennis Koch
Dennis Kochβ€’17mo ago
You don't have record on a Column basis How would you hide columns on a row level inside a table? πŸ€” That would break the layout
toeknee
toekneeβ€’17mo ago
Exactly, you need to format the state on the condition