F
Filament2mo ago
MZX

Existing data not showing in the select box.

When I click on a record to edit it, the select boxes do not show the data which should be there. This is my code
return $form
->schema([
Select::make('level_id')
->label('Level')
->options(fn () => Level::all()->pluck('name', 'id')) // Fetch levels dynamically
->required()
->reactive() // React to changes in Level
->afterStateUpdated(fn (callable $set) => $set('grade_id', null)), // Reset grade when level changes

Select::make('grade_id')
->label('Grade')
->options(function (callable $get) {
$levelId = $get('level_id'); // Get the selected level_id
if ($levelId) {
return Grade::where('level_id', $levelId)->pluck('name', 'id'); // Fetch grades based on selected level
}
return [];
})
->required()
->reactive() // React to changes in Grade
->afterStateUpdated(fn (callable $set) => $set('section_id', null)), // Reset section when grade changes

Select::make('section_id')
->label('Section')
->options(function (callable $get) {
$gradeId = $get('grade_id'); // Get the selected grade_id
if ($gradeId) {
return Section::where('grade_id', $gradeId)->pluck('name', 'id'); // Fetch sections based on selected grade
}
return [];
})
->required(),
]);
return $form
->schema([
Select::make('level_id')
->label('Level')
->options(fn () => Level::all()->pluck('name', 'id')) // Fetch levels dynamically
->required()
->reactive() // React to changes in Level
->afterStateUpdated(fn (callable $set) => $set('grade_id', null)), // Reset grade when level changes

Select::make('grade_id')
->label('Grade')
->options(function (callable $get) {
$levelId = $get('level_id'); // Get the selected level_id
if ($levelId) {
return Grade::where('level_id', $levelId)->pluck('name', 'id'); // Fetch grades based on selected level
}
return [];
})
->required()
->reactive() // React to changes in Grade
->afterStateUpdated(fn (callable $set) => $set('section_id', null)), // Reset section when grade changes

Select::make('section_id')
->label('Section')
->options(function (callable $get) {
$gradeId = $get('grade_id'); // Get the selected grade_id
if ($gradeId) {
return Section::where('grade_id', $gradeId)->pluck('name', 'id'); // Fetch sections based on selected grade
}
return [];
})
->required(),
]);
12 Replies
Matthew
Matthew2mo ago
What does your edit firing code look like ?
MZX
MZX2mo ago
What do you mean? This is just when you click on a record. The select fields should show the existing value but instead they're not set.
Matthew
Matthew2mo ago
Yes, but you'll have had to include something to make that click an action. In your resource, what does your
actions
actions
chained method contain in the
table
table
function?
MZX
MZX2mo ago
You mean this?
Tables\Actions\EditAction::make(),
Tables\Actions\EditAction::make(),
LeandroFerreira
LeandroFerreira2mo ago
What select? Are you using the panel builder or only the form builder?
MZX
MZX2mo ago
panel builder Its inside the Resource Levels have Grades which have Sections Student belongs to a Section I made it work by defining a relationship between Student and Grade, and Student and Level but its redundant because a student already belongs to a section, and the section belongs to grade which belongs to level panel builder
LeandroFerreira
LeandroFerreira2mo ago
if you have a relationship, you should check this section you can see an example of dependent selects here
MZX
MZX2mo ago
I did check that, but i have a third degree dependency hope you understand like first i want to choose the level, which will show the relavant grades, which will then show the relavant sections Level and Grade are not related to the Student directly, but through Section
LeandroFerreira
LeandroFerreira2mo ago
are you using Filament v2? Not related to the issue, but double-check your code if you are using Filament v3. reactive() and callable are used in v2. Ensure your select query returns the expected results..
Matthew
Matthew2mo ago
So do level_id, grade_id & section_id sit as columns in the Student model?
MZX
MZX2mo ago
V3 Currently yes, to make it work properly. Else ideally I'd like just the section_id in the student model I think for a filament form to be able to fetch a value, it needs to be a column in the table, and since grade and level aren't in the table but referenced through section, it is not able to fetch the value which explains why it works when i add the grade and level column to student makes it redundant in a way but can't see any other way
Matthew
Matthew2mo ago
No. The way you've coded it, it needs to be in the table. You need to to learn how Filament accesses relationships: https://filamentphp.com/docs/3.x/forms/advanced#saving-data-to-relationships
Want results from more Discord servers?
Add your server