Laravel Filament Bulk action: how to set form and get values from there

592 Views Asked by At

I have a Filament Resource with a Bulk Action with modal confirm, and i have a form there to add some values for selected records. How i can access to the values of this form in action method?

<?php
$table->bulkActions([
    \Filament\Tables\Actions\BulkAction::make('my_action')
        ->requiresConfirmation()
        ->form(function (Collection $records) {
            \Filament\Forms\Components\TextInput::make('name')
        })
        ->action(function (Collection $records) {
            //i need a value of form TextInput [name] here
        });
]);
1

There are 1 best solutions below

0
Igor On

Just now i found the answer how to get form data/values from modal: via table Livewire object. Use $table->getLivewire()->getMountedTableBulkActionForm()->getState().

Somewhere in your Filament Resource:

<?php
use Filament\Forms\Components\TextInput;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Table;
use Illuminate\Support\Collection;

public static function table(Table $table): Table
{
    return $table
        ->bulkActions([
            BulkAction::make('my_bulk_action')
                ->form(function (Collection $records) {
                    return [
                        TextInput::make('name')
                    ];
                })
                ->action(function (Collection $records) use ($table) {
                    $data = $table->getLivewire()->getMountedTableBulkActionForm()->getState();
                    // will return TextInput value in $data['name']
                });
        ]);
}

Hope that helps.