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")...
Jump to solution
4 Replies
_andypeacock
_andypeacockOP14mo ago
Hmm. Nearly there:
Select::make('roles')
->label("Role")
->multiple(false)
->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();
logger("roles", [ "data" => $roles ]);
$component->state($roles);
})
->default('User')
// REMOVING THIS MEANS $roles IS CORRECT
//->relationship('roles', 'name')
->required(),
Select::make('roles')
->label("Role")
->multiple(false)
->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();
logger("roles", [ "data" => $roles ]);
$component->state($roles);
})
->default('User')
// REMOVING THIS MEANS $roles IS CORRECT
//->relationship('roles', 'name')
->required(),
$roles is set to: array:3 [ 3 => "User" 2 => "Account Admin" 1 => "Super Admin" ] But now I have no options in the select - just the "Select an option" entry.
Solution
_andypeacock
_andypeacock14mo ago
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(),
_andypeacock
_andypeacockOP14mo ago
Thanks so much!
Want results from more Discord servers?
Add your server