F
Filament•15mo ago
MAF

How can I pass $state to a static function for a reusable Column/Input?

I have 6 columns for different states. But all of them follow the same convention. So, I would rather not keep repeating the code. I created the following static method.
public static function stateColumn($state): TextColumn
{
return TextColumn::make($state)
->formatStateUsing(fn ($state) => ucfirst($state))
->color([
'danger' => 'failed',
'warning' => 'processing',
'success' => fn ($state) => in_array($state, ['ready'])
]);
}
public static function stateColumn($state): TextColumn
{
return TextColumn::make($state)
->formatStateUsing(fn ($state) => ucfirst($state))
->color([
'danger' => 'failed',
'warning' => 'processing',
'success' => fn ($state) => in_array($state, ['ready'])
]);
}
Now, I want to call this static function within columns array. One thing I found, is a recent video posted on #💫┊announcements about enums. But this will be a lot of code, too. I prefer a single function.
3 Replies
awcodes
awcodes•15mo ago
You don’t need to pass state to the column. Just the column / field name. The $state in the method callbacks will be injected based on the name in make()
MAF
MAFOP•15mo ago
But the column/field name is being passed as a string to the function. So, you can't access the value.
awcodes
awcodes•15mo ago
You don’t need to access the value. On my phone so can’t really provide an example at the moment.
Want results from more Discord servers?
Add your server