skip to Main Content

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


  1. Please, do read the documentation again…

    Your code is:

    ->dependsOn(
       'hmo',
    

    It should be:

    ->dependsOn(
        ['hmo'],
    

    Documentation says the first argument must be an array, you are passing a text…

    Login or Signup to reply.
  2. 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.

    function (Select $field, NovaRequest $request, FormData $formData) {
      if ($formData->hmo === null)
        $field->hide();
      else
        $field->show()->options(
        AppModelsHmoPlan::where('hmo_id',  $formData->hmo)
          ->get()
          ->mapWithKeys(fn ($hmo) => [
            $hmo->id => $hmo->name
          ])
        );
    }
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search