I am working on dependent fields so that what was selected will be used in the next dropdown. dependsOn does not work, i am using nova version 4 and php 8. The function in the dependsOn flag does not fire when i select an item in the hmo field.
Ref https://nova.laravel.com/docs/4.0/resources/fields.html#dependent-fields
BelongsTo::make('Hmo', 'hmo',)
->filterable(),
Select::make('Enrollee Plan')
->hide()
->filterable()
->dependsOn(
'hmo',
function (Select $field, NovaRequest $request, FormData $formData) {
Log::info("messagessss");
if ($formData->hmo === null) {
$field->hide();
}
$field->show()->options(
HmoPlan::where('hmo_id', $formData->hmo)
->get()
->mapWithKeys(fn ($hmo) => [
$hmo->id => $hmo->name
])
);
}
),
2
Answers
Please, do read the documentation again…
Your code is:
It should be:
Documentation says the first argument must be an
array
, you are passing a text…I am missing either the "else" statement or removing show() after $field->hide(). In the code provided in the question, the field will be always shown.
Also, the problem with the Select field is that it does not support ->showCreateRelationButton(), compared by the BelongsTo field, which supports it.
But, BelongsTo field does not support the options() method, which is unfortunate, and looking for native support for relatable fields filtering in Laravel Nova.