OCTOBERCMS Dropdown options dependent on selected value on other dropdown

Ad

I'm stuck with this problem and i can't figure out how to solve after some time searching for a example.

The two dropdowns options are table dependent on their values.

I have the one table with 'area' values (nested with simple tree working ok) with the following structure on fields.yaml file:

fields:
  id:
    label: Número
    oc.commentPosition: ''
    span: auto
    disabled: 1
    type: number

  area_id:
    label: 'Parente de'
    oc.commentPosition: ''
    emptyOption: 'Sem valor'
    span: auto
    type: dropdown

  area:
    label: Área
    oc.commentPosition: ''
    span: full
    required: 1
    type: text

I also have another table 'modulos' values with the following structure in fields.yaml:

fields:
  modulo:
    label: Módulo
    oc.commentPosition: ''
    span: auto
    required: 1
    type: text

  area:
    label: Área
    oc.commentPosition: ''
    nameFrom: area
    emptyOption: 'Sem valor'
    span: auto
    descriptionFrom: id
    type: relation

In the 'Area' model I have:

 ... 
 public $hasMany = [
    'modulos' => ['JML\Gkb\Models\Modulos']
 ];

In the 'Modulos' model I have

....
 public $belongsTo = [
    'area' => ['\JML\Gkb\Models\Area']
];

I have other model that have relations with previous fields and two dropdown fields working ok without any filter, and the troubleshoting field (modulos) where I can't find a way to filter based on the values of 'Area' dropdown I have the following in fields.yaml.

....
modulo_id:
  label: mod
  oc.commentPosition: ''
  emptyOption: 'Sem valor'
  span: auto
  required: 1
  dependsOn:

area
  type: dropdown
  tab: Geral

In my models PHP file where I have the dropdowns defined, I have:

public function getModuloIdOptions() {
    return Modulos::where('area_id', '=', $this->area)->lists('modulo', 'id');
}

That to me seems logical (maybe not) and I tried with DB also and many more other. I tried with dd() to see if I can get the values from the first dropdown to no avail. If I try to filter the values, no value appears at all (except an empty value).

Any help out there ???

TIA

JL

Ad

Answer

Ad

The dataset is passed as the second argument to get the "getOptions" method. Here is an alternative approach that may work:

public function getModuloIdOptions($value, $data) {
    return Modulos::where('area_id', '=', array_get($data, 'area'))->lists('modulo', 'id');
}

You may also want to try accessing the area_id value:

public function getModuloIdOptions(){
    return Modulos::where('area_id', '=', $this->area_id)->lists('modulo', 'id');
}

Or less efficiently the area->id value (may require exception handling):

public function getModuloIdOptions(){
    return Modulos::where('area_id', '=', $this->area->id)->lists('modulo', 'id');
}
Ad
source: stackoverflow.com
Ad