Multi-select select all/deselect all

Is there any better solution to select all/deselect all options then the following, needs help to refactor the toggeling
Select::make('departments')
    ->required()
    ->multiple()
    ->searchable()
    ->preload()
    ->relationship(titleAttribute: 'name')
    ->hintActions([
        Action::make('select_all')
            ->visible(fn($state) => empty($state))
            ->action(fn (Select $component) => $component->state(Department::pluck('id')->toArray())),

        Action::make('deselect_all')
            ->hidden(fn($state) => empty($state))
            ->action(function (Set $set) {
                $set('departments', null);
            }),
    ])
Was this page helpful?