© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•16mo ago•
4 replies
Jamie Cee

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
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

MorphToMany Relationship in filament select option
FilamentFFilament / ❓┊help
3y ago
Select relationship
FilamentFFilament / ❓┊help
3y ago
How to save morphToMany relationship during create?
FilamentFFilament / ❓┊help
2y ago