ziffmedia / nova-select-plus

A Laravel Nova Select Field
MIT License
91 stars 26 forks source link

Using as an Action field #67

Open jolora opened 6 months ago

jolora commented 6 months ago

First of all, thanks for sharing this great package!

Is the SelectPlus field compatible with Nova Actions?

I have a simple Nova Action for updating a manytomany relation on a model:

class UpdateConsortia extends Action
{
    use InteractsWithQueue, Queueable;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
    foreach ($models as $model) {
        $model->consortia()->sync($fields->consortia);
            $model->save();
        }
    }

    /**
     * Get the fields available on the action.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
    $consortia = Consortium::pluck('id', 'name');
    $formattedConsortia = $consortia->mapWithKeys(function ($id, $name) {
        return [$id => ['label' => $name]];
    });

        return [
        MultiSelect::make('Consortia')->options($formattedConsortia)->displayUsingLabels(),
    ];
    }
}

I'd like to swap the MultiSelect field for a SelectPlus field like this:

public function fields(NovaRequest $request)
{
    return [
        SelectPlus::make('Consortia', 'consortia', Consortium::class)
            ->usingDetailLabel('name')
            ->label(fn ($consortium) => $consortium->name . "")
    ];
}

However, although the field renders fine $fields->consortia always returns null. The exact same field works fine used directly on the resource page.