Nova dependsOn does not work using dependent filters

huangapple go评论66阅读模式
英文:

Nova dependsOn does not work using dependent filters

问题

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.

参考链接: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
            ])
            );
        }
    ),
英文:

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
            ])
            );
        }
    ),

答案1

得分: 1

请再次阅读文档...

你的代码是:

->dependsOn(
   'hmo',

应该是:

->dependsOn(
    ['hmo'],

文档说第一个参数必须是一个array,而你传递的是文本...

英文:

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...

答案2

得分: 0

我缺少“else”语句或在$field->hide()后删除show()。在问题中提供的代码中,该字段将始终显示。

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

此外,Select字段的问题在于它不支持->showCreateRelationButton(),与BelongsTo字段相比,后者支持该功能。但是,BelongsTo字段不支持options()方法,这是不幸的,因此正在寻找Laravel Nova中可关联字段过滤的本地支持。

英文:

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(
    \App\Models\HmoPlan::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.

huangapple
  • 本文由 发表于 2023年3月9日 15:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定