Live select field not found with $get

In a create form, I have a select field called base_position_id defined as
Select::make('base_position_id')
->label('Position')
->placeholder('Select a Base Position from DGP')
->options(function () {
return BasePosition::all()->sortBy('jobTitle')->pluck('jobTitle', 'id')
->filter(fn($value, $key) => Position::where('base_position_id', $key)
->get()
->isEmpty());
})
->required()
->live(),
Select::make('base_position_id')
->label('Position')
->placeholder('Select a Base Position from DGP')
->options(function () {
return BasePosition::all()->sortBy('jobTitle')->pluck('jobTitle', 'id')
->filter(fn($value, $key) => Position::where('base_position_id', $key)
->get()
->isEmpty());
})
->required()
->live(),
Right below that, I have this inside a Split and a Section:
Placeholder::make('code')
->label('Position Code:')
->inlineLabel()
->content(fn(Get $get) => $get('base_position_id') ?: '')
->disabled(),
Placeholder::make('code')
->label('Position Code:')
->inlineLabel()
->content(fn(Get $get) => $get('base_position_id') ?: '')
->disabled(),
which works perfectly. So I know base_position_id is getting set correctly and should be available. But a little farther down I have another select field that I'd like to filter based on base_position_id.
Grid::make()
->relationship('alt_manager_code')
->columns(1)
->schema([
Select::make('managerCode')
->label('Override Hiring Manager')
->placeholder('Select Position to Override Manager')
->options(function (Get $get) {
info($get('base_position_id')); // LOG TO SEE IF VALUE IS AVAILABLE
return BasePosition::all()
->sortBy('jobTitle')
->pluck('jobTitle', 'id');
}),
]),
Grid::make()
->relationship('alt_manager_code')
->columns(1)
->schema([
Select::make('managerCode')
->label('Override Hiring Manager')
->placeholder('Select Position to Override Manager')
->options(function (Get $get) {
info($get('base_position_id')); // LOG TO SEE IF VALUE IS AVAILABLE
return BasePosition::all()
->sortBy('jobTitle')
->pluck('jobTitle', 'id');
}),
]),
Obviously on first load, the logged value is empty. But when I select a base_position_id and the field refreshes, it still logs a blank value. Shouldn't I be getting the selected value at that point? I need it to add a filter to the return collection.
Solution:
sorry, i miss-read where the relationship was. try $get('../base_position_id') since you are in a layout component you need to traverse out of it to get the state from other fields.
Jump to solution
11 Replies
phydeaux
phydeauxOP5mo ago
Also, if I dd($get) at the point of the log line, I have a Get object with only one component, the current select field itself.
gemini.dev
gemini.dev5mo ago
Hi, I'm no expert in filament, but maybe this could help: Instead of: ->options(function (Get $get) { Can you try: ->options(function (callable $get) {
phydeaux
phydeauxOP5mo ago
I appreciate the suggestion. Sadly, no joy. Same result. Or rather same lack of result.
awcodes
awcodes5mo ago
Options() and relationship() don’t work together. It should be one or the other. The third parameter of relationship() can be used to modify the query to “filter” results. https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-relationship-query
phydeaux
phydeauxOP5mo ago
Maybe what I'm trying to do isn't possible then. The way I understand it is that relationship() on the Select is used to populate the options and then save that relation to the primary model and relationship() on the layout components is for saving fields to relationships. In my case, the relationship on the Grid is there to have a place to (optionally) save the value if one is selected - an override of the default value currently assigned - but the select list options are coming from an entirely different model. I'm pulling those options in just fine. What I'm trying to do is to filter out the selected code as well as the currently assigned manager from the list since the override value can't be either of those.
Solution
awcodes
awcodes5mo ago
sorry, i miss-read where the relationship was. try $get('../base_position_id') since you are in a layout component you need to traverse out of it to get the state from other fields.
phydeaux
phydeauxOP5mo ago
Ah, so Get is scoped to the layout component? Good to know.
awcodes
awcodes5mo ago
yea, same with Set trying to find the docs about it. might be better to think of it as scoped to the level of the component in the data array, if that makes sense. but it can be traversed similar to traversing a directory structure
phydeaux
phydeauxOP5mo ago
Okay, cool. Thanks. I didn't think that was an issue since I was able to access it inside a Split >> Section >> Placeholder structure. Anyway, It's something to try next time I run up against it. And that worked great, btw
awcodes
awcodes5mo ago
You’re split and section probably aren’t using relationships so the data wouldn’t be nested. Cheers.
phydeaux
phydeauxOP5mo ago
That makes sense, thanks
Want results from more Discord servers?
Add your server