Save MM relationship with single select

Hi,

I have a condition in my form that checks if a user has only permission to one "location" and if so, the select multiple should render as single select where the single location value is already selected. This is easy by just querying for one location and set it as default. But because the data structure expects a many to many relation I need to modify the save method to attach one single location to the MM table.

I tried it like this, but that doesn't work:
            return Forms\Components\Select::make('locations')
                ->label(trans('locations.singular'))
                ->disabled()
                ->options(Auth::user()->locations()->get()->pluck('name', 'id'))
                ->saveRelationshipsUsing(fn($data, $record) => $record->locations()->sync($data['locations']))
                ->disablePlaceholderSelection();


How should the code look like?
Thank you!
Was this page helpful?