Select with BelongsToMany without multiple()?

Trying to work with an existing model setup. Have two relationships. One just gets a list of contacts and you can pick multiple. The other lets you simple pick 1 as your primary contact. They are both stored in the same table, there is just an additional boolean column for setting it as primary or not. Everything works great for loading the relationship and saving at this point, but I obviously dont want multiple() for the primary contact. Suggestions? The primary relationship is something like
$this->contacts()->wherePivot('primary', 1);
$this->contacts()->wherePivot('primary', 1);
with ->contacts() being a belongstomany(). Im using saveRelationshipsUsing() to handle the saving part. Like I said, everything works great except for multiple() requirement as Select wont work with belongstomany().
7 Replies
Dan Harrin
Dan Harrin2y ago
i think its a flaw in the db structure if its not possible in the business domain, it shouldnt be possible in the db you should use a primary_contact_id on the table and then a simple BelongsTo the only other way you could do it is creating a select dehydrated(false), manually defining options, and then inside saveRelationshipsUsing() use $get and attach the pivot.
Select::make('primary_housing_locator')
->label('Primary Housing Locator')
->options($housingLocators)
->dehydrated(false)
->searchable(),
Select::make('housing_locators')
->multiple()
->label('Other Housing Locators')
->relationship('housing_locators', 'id')
->saveRelationshipsUsing(static function (Select $component, $state, $get) {
// $get('primary_housing_locator');

$component->getRelationship()->syncWithPivotValues($state, ['type' => 'Housing Locator', 'primary' => 0]);
})
->columnSpan(2),
Select::make('primary_housing_locator')
->label('Primary Housing Locator')
->options($housingLocators)
->dehydrated(false)
->searchable(),
Select::make('housing_locators')
->multiple()
->label('Other Housing Locators')
->relationship('housing_locators', 'id')
->saveRelationshipsUsing(static function (Select $component, $state, $get) {
// $get('primary_housing_locator');

$component->getRelationship()->syncWithPivotValues($state, ['type' => 'Housing Locator', 'primary' => 0]);
})
->columnSpan(2),
Mark Chaney
Mark ChaneyOP2y ago
how is the existing record selected for the primary_housing_locator?
Dan Harrin
Dan Harrin2y ago
select the primary, and then the rest oh you mean when loading? idk, you choose just fill your form with the primary locator its a custom form so you choose how to fill that property
Mark Chaney
Mark ChaneyOP2y ago
its not really a custom form, im just filling it with EditRecord on a table. I can modify what its given though I guess.
Dan Harrin
Dan Harrin2y ago
EditAction? i think theres a mutate function for that
Mark Chaney
Mark ChaneyOP2y ago
How do i even fill a select without a relationship? I have options taken care of, but how do select the existing value? passing an id just puts an id in the box, but thats obviously not what i want displayed.
awcodes
awcodes2y ago
If there’s an option with that key then setting the selection is automatic based on the data.
Want results from more Discord servers?
Add your server