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(),


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);
                    })

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(),
Was this page helpful?