© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
6 replies
_andypeacock

Change case of relationship name field in a select

Hi all,

Looking for a bit of help on this one: I've got a Select field which works fine with a relationship:

Select::make('roles')
                    ->label("Role")
                    ->multiple(false)
                    ->default('user')
                    ->relationship('roles', 'name')
                    ->required(),
Select::make('roles')
                    ->label("Role")
                    ->multiple(false)
                    ->default('user')
                    ->relationship('roles', 'name')
                    ->required(),


However, the role names are slug format (super_user, account_admin) etc, and I'd like them to appear in the select as "Super User", "Account Admin", etc.

I thought this would work:

                    ->loadStateFromRelationshipsUsing(function (Select $component, $state) use ($highestLevel) {
                        $roles = Role::where('level', '<=', $highestLevel)
                            ->orWhere('level', 1)
                            ->orderBy('level')
                            ->pluck('name', 'id')
                            ->map(fn ($role) => ucwords(str_replace('_', ' ', $role)))
                            ->toArray();
                        $component->state($roles);
                    })
                    ->loadStateFromRelationshipsUsing(function (Select $component, $state) use ($highestLevel) {
                        $roles = Role::where('level', '<=', $highestLevel)
                            ->orWhere('level', 1)
                            ->orderBy('level')
                            ->pluck('name', 'id')
                            ->map(fn ($role) => ucwords(str_replace('_', ' ', $role)))
                            ->toArray();
                        $component->state($roles);
                    })

But the if I add a logger() statement into the function, it's not even getting called.

Any ideas how I can do this?

Thanks in advance,
Andy
Solution
Ah, got there, through a different approach (which obviously I'm sure I tried the first time, but c'est la vie):

Select::make('roles')
                    ->label("Role")
                    ->multiple(false)
                    ->default('User')
                    ->relationship('roles', 'name')
                    ->options(
                        Role::where('level', '<=', $highestLevel)
                            ->orWhere('level', 1)
                            ->orderBy('level')
                            ->pluck('name', 'id')
                            ->map(fn ($role) => ucwords(str_replace('_', ' ', $role)))
                            ->toArray()
                    )
                    ->required(),
Select::make('roles')
                    ->label("Role")
                    ->multiple(false)
                    ->default('User')
                    ->relationship('roles', 'name')
                    ->options(
                        Role::where('level', '<=', $highestLevel)
                            ->orWhere('level', 1)
                            ->orderBy('level')
                            ->pluck('name', 'id')
                            ->map(fn ($role) => ucwords(str_replace('_', ' ', $role)))
                            ->toArray()
                    )
                    ->required(),
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

SelectFilter relationship field
FilamentFFilament / ❓┊help
3y ago
Grouping options of a Select field with a relationship.
FilamentFFilament / ❓┊help
3y ago
Show name instead of id in Select relationship field, upon record soft deletion.
FilamentFFilament / ❓┊help
2y ago
Reorder on Select field relationship
FilamentFFilament / ❓┊help
2y ago