Custom action in relation manager

Hello - is it possible to pass a parameter in a custom action in a relation manager table?
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('item.name'),
Tables\Columns\TextColumn::make('qty')
->label('Quantity'),
])
->filters([
//
])
->headerActions([
// Tables\Actions\CreateAction::make(),
])
->actions([
// Tables\Actions\EditAction::make(),
// Tables\Actions\DeleteAction::make(),
Tables\Actions\Action::make('refundItem')
->label('Refund Item')
->action(function($record) {
return "refundItem($record->id)";
})
])
->bulkActions([
// Tables\Actions\DeleteBulkAction::make(),
]);
}

public function refundItem($selectionId)
{
dd($selectionId);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('item.name'),
Tables\Columns\TextColumn::make('qty')
->label('Quantity'),
])
->filters([
//
])
->headerActions([
// Tables\Actions\CreateAction::make(),
])
->actions([
// Tables\Actions\EditAction::make(),
// Tables\Actions\DeleteAction::make(),
Tables\Actions\Action::make('refundItem')
->label('Refund Item')
->action(function($record) {
return "refundItem($record->id)";
})
])
->bulkActions([
// Tables\Actions\DeleteBulkAction::make(),
]);
}

public function refundItem($selectionId)
{
dd($selectionId);
}
I can use a custom action without a parameter. When I add the record ID as a parameter I see two requests but neither one seems to work
4 Replies
Dennis Koch
Dennis Koch2y ago
You return a string? Put your logic into the callback and it should work
bwurtz999
bwurtz999OP2y ago
Oh ok So I can't pass it to a different function? It all has to be done in the callback?
Dennis Koch
Dennis Koch2y ago
A different function can also be a callback. If you use PHP8.1 you can use the new callable syntax: https://www.php.net/manual/en/functions.first_class_callable_syntax.php E.g. ->action(YourResource::refundItem(...))
bwurtz999
bwurtz999OP2y ago
Works perfectly Thank you!
Want results from more Discord servers?
Add your server