MorphToMany Select Relationship

Im getting completely frazzled with MorphToMany setup in a select instance. I have a User that Morphs to Many Organisation and Product. Im trying to merge the items into a single Select, and I have it working, except when trying to edit a user, I cant get it to show the already existing records as already selected values. I found this function
->loadStateFromRelationshipsUsing(function (User $record) {
// Retrieve the organisation IDs managed by the user
$organisationIds = $record->morphedOrganisations()->pluck(
(new Organisation())->getQualifiedKeyName()
)->toArray();

// Retrieve the product IDs managed by the user
$productIds = $record->morphedProducts()->pluck(
(new Product())->getQualifiedKeyName()
)->toArray();

dd(array_merge($organisationIds, $productIds));

// Combine both arrays of IDs
return array_merge($organisationIds, $productIds);
})
->loadStateFromRelationshipsUsing(function (User $record) {
// Retrieve the organisation IDs managed by the user
$organisationIds = $record->morphedOrganisations()->pluck(
(new Organisation())->getQualifiedKeyName()
)->toArray();

// Retrieve the product IDs managed by the user
$productIds = $record->morphedProducts()->pluck(
(new Product())->getQualifiedKeyName()
)->toArray();

dd(array_merge($organisationIds, $productIds));

// Combine both arrays of IDs
return array_merge($organisationIds, $productIds);
})
I can get the array of ids, but im not sure what im missing at displaying them as the selected values
Solution:
Ah, I figured it out, I don't return it, I attach to $component->state();
Jump to solution
2 Replies
Jamie Cee
Jamie CeeOP2mo ago
Should I be returning an array of the values in my select? so the IDs I retrieve?
Solution
Jamie Cee
Jamie Cee2mo ago
Ah, I figured it out, I don't return it, I attach to $component->state();
Want results from more Discord servers?
Add your server