Select::make() get the inner html

Select::make('language_id')
->relationship('language', 'slug')
->afterStateUpdated(function(?string $state, ?string $old) {}
Select::make('language_id')
->relationship('language', 'slug')
->afterStateUpdated(function(?string $state, ?string $old) {}
$state and $old get the value="" like 1,2,3 etc but how do I get the value the user sees like <option value="1"> how_do_i_get_this </option>
Solution:
would you like to get the label? ```php ->afterStateUpdated(function (?string $state, Set $set, Select $component) { $set('code-editor', $component->getOptionLabel());...
Jump to solution
21 Replies
Dennis Koch
Dennis Koch3mo ago
You can't get the label. That's not how HTML forms work.
Giant Robo
Giant RoboOP3mo ago
so I need to do an eloquent query and get the label from the db?
Dennis Koch
Dennis Koch3mo ago
Yes.
Giant Robo
Giant RoboOP3mo ago
can I preload this data somehow? if I query everytime afterStateUpdated(), that might be a lot
Dennis Koch
Dennis Koch3mo ago
When do you want to preload it? Where else are you using that data?
Giant Robo
Giant RoboOP3mo ago
it's a list of languages for programming languages,
Select::make('language_id')
->relationship('language', 'slug')
->afterStateUpdated(function(?string $state, ?string $old,Set $set, Get $get) {

//$set('code-editor', set new language here)
Select::make('language_id')
->relationship('language', 'slug')
->afterStateUpdated(function(?string $state, ?string $old,Set $set, Get $get) {

//$set('code-editor', set new language here)
there is a Markdown editor on the page I want to insert the newly selected language into the editor
MarkdownEditor::make('code-editor')
MarkdownEditor::make('code-editor')
in this editor, adding '''javascript will change the code formatting to javascript, etc
Dennis Koch
Dennis Koch3mo ago
Do you even need the value then? What do you use slug for?
Giant Robo
Giant RoboOP3mo ago
the slug is the <option value="id"> slug </option> that the user sees
Dennis Koch
Dennis Koch3mo ago
I mean: Do you need both? Otherwise just change what you use as value
Giant Robo
Giant RoboOP3mo ago
you can change the value filament uses?
Dennis Koch
Dennis Koch3mo ago
Btw. you shouldn't use ->relationship() if you don't want to save it to a relationship Just use ->options([])
Giant Robo
Giant RoboOP3mo ago
oh yes it is a relationship $code->language->slug it's got a pivot table the Select::make works perfectly out of the box pretty amazing
Dennis Koch
Dennis Koch3mo ago
So you want to insert this into the editor but also save it to the DB?
Giant Robo
Giant RoboOP3mo ago
No description
Giant Robo
Giant RoboOP3mo ago
see the '''php when the user changes the language with the Select I want to replace that '''php with '''new language to trigger the code highlighting
Dennis Koch
Dennis Koch3mo ago
Yeah, I don't think there is an easier way then load it from DB and replace it
Giant Robo
Giant RoboOP3mo ago
so can I query the db just once and store the data somewhere? I tried to do this on the resource, but it didn't work if not, I will have to query every time the user changes the Select
Solution
LeandroFerreira
LeandroFerreira3mo ago
would you like to get the label?
->afterStateUpdated(function (?string $state, Set $set, Select $component) {
$set('code-editor', $component->getOptionLabel());
})
->afterStateUpdated(function (?string $state, Set $set, Select $component) {
$set('code-editor', $component->getOptionLabel());
})
?
Giant Robo
Giant RoboOP3mo ago
let me try this code @Filament legendary, it worked oops @Leandro Ferreira legendary
Dennis Koch
Dennis Koch3mo ago
Good to know
Giant Robo
Giant RoboOP3mo ago
@Dennis Koch thanks for your help I'm just loving this filament

Did you find this page helpful?