Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using as an Action field #67

Open
jolora opened this issue Jan 26, 2024 · 0 comments
Open

Using as an Action field #67

jolora opened this issue Jan 26, 2024 · 0 comments

Comments

@jolora
Copy link

jolora commented Jan 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant