FilamentF
Filament15mo ago
Matthew

Pass Model to re-useable Table Action

I've got a delete action which is repeated in various places.

I want write the component in a helper class and then call it into the getTableActions array as required.

To do this, I need to pass the model, but I can't make it work, it is passing a closure.

Tried lots of iterations like this:

    public static function getTableActions() : array
    {
        return [

            TableAction::deleteApplication((function (Model $record) : Model {

                return $record;
            })
Solution
The handler is separate.

I've worked it out!

I was overcomplicating this...I thought I'd have to pass the $record into deleteApplication function so it can return the Action::make('deleteApplication')

Which makes sense, until you think about for longer than 5 minutes. The $record attribute is being injected in the Action::make() function, and therefore it is fine just to leave $record in the normal places, so the trait looks like:

`''trait Application
{
public static function deleteApplication() : Action
{

return Action::make('deleteApplication')
->label('')
->visible(function ($record) {
return $record->canBeDeleted();
})
->requiresConfirmation()
->action(function (DeleteApplicationHandler $handler, $record) {
$handler->handle($record);
});

}
}'''

In conclusion, the answer to the question how to "Pass Model to re-useable Table Action" is....you don't need to there, it is already available.

Apologies for wasting your time !
Was this page helpful?