How to pass record to table row action?

Hi guys, I am trying dynamically create actions for my table rows:
public function table(Table $table): Table
{
return parent::table($table)
->columns([...])
->actions([
Tables\Actions\ActionGroup::make([
[...$this->getContractStatusActions(fn (Contract $record) => $record)],
]),
]);
}

private function getContractStatusActions(\Closure $closure): array
{
/** @var Contract $contract */
$contract = $closure();

$nextState = $contract->status->getNextState();

return collect($nextState)
->map(function (ContractStatus $state) use ($contract) {
return Tables\Actions\Action::make('updateStatus')
->label($state->getLabel())
->action(fn () => $contract->update(['status' => $state]));
})
->toArray();
}
public function table(Table $table): Table
{
return parent::table($table)
->columns([...])
->actions([
Tables\Actions\ActionGroup::make([
[...$this->getContractStatusActions(fn (Contract $record) => $record)],
]),
]);
}

private function getContractStatusActions(\Closure $closure): array
{
/** @var Contract $contract */
$contract = $closure();

$nextState = $contract->status->getNextState();

return collect($nextState)
->map(function (ContractStatus $state) use ($contract) {
return Tables\Actions\Action::make('updateStatus')
->label($state->getLabel())
->action(fn () => $contract->update(['status' => $state]));
})
->toArray();
}
I am still getting this error: Too few arguments to function App\Filament\Resources\ContractResource\Pages\ListContracts::App\Filament\Resources\ContractResource\Pages\{closure}(), 0 passed in /Users/me/Developer/Sites/leasing/app/Filament/Resources/ContractResource/Pages/ListContracts.php on line 91 and exactly 1 expected
3 Replies
Trauma Zombie
Trauma Zombie4mo ago
Anyone can help me?
Dennis Koch
Dennis Koch4mo ago
Use ->visible(fn ($record) => instead The records aren't available when your code is evaluated
Trauma Zombie
Trauma Zombie4mo ago
Hi Dennis, thank you. But I need to create these actions dynamically, because there is too many states and I want to create actions based on the next possible transitions, states.