Yes, this is possible! In Filament, bulk actions can include a modal form where you prompt for additional input before running the action. In your case, you want to let the user select a new contract.
You do this via the ->form() method on your bulk action. Here’s how you can update your bulk action to prompt the user to select a contract before moving the works:
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Tables\Actions\BulkAction;
use Illuminate\Support\Collection;
BulkAction::make('move')
->icon(Heroicon::ArrowRight)
->label('Move to contract')
// Add a modal form to select the new contract
->form([
Forms\Components\Select::make('new_contract_id')
->label('Target Contract')
->options(\App\Models\Contract::pluck('name', 'id'))
->required(),
])
->action(function (Collection $records, array $data) {
$thisContract = \App\Models\Contract::find($this->ownerRecord->id);
$newContract = \App\Models\Contract::find($data['new_contract_id']);
$worksMovedCount = 0;
foreach ($records as $record) {
$newContract->works()->attach($record->id);
$thisContract->works()->detach($record->id);
$worksMovedCount++;
}
if ($worksMovedCount > 0) {
Notification::make()
->success()
->title('Works moved')
->body($worksMovedCount . ' works were moved to the new contract')
->send();
}
});
Key Points:
- The
->form([...])method adds input fields to the modal, shown to the user when they initiate the bulk action. - The value the user selects is available in the
$dataarray passed to the action. - Replace
\App\Models\Contract::pluck('name', 'id')with whatever field you use to identify the contract in your database.
With this approach, the user can choose which contract to move works to each time they use the bulk action!