Laravel Filament 批量操作:如何设置表单并从中获取值

问题描述 投票:0回答:1

我有一个带有模态确认的批量操作的灯丝资源,并且我有一个表单可以为选定的记录添加一些值。我如何在

action
方法中访问此表单的值?

<?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
        });
]);
laravel forms action laravel-livewire laravel-filament
1个回答
0
投票

刚才我找到了如何从模态获取表单数据/值的答案:通过表

Livewire
对象。使用
$table->getLivewire()->getMountedTableBulkActionForm()->getState()

您的灯丝资源中的某处:

<?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']
                });
        ]);
}

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.